我有两个文件:file1和file2,如下所示:
file1 file2 orangejuice orangejuice_9.88_9.88 pineapplejuice appleslices_6.3_2.2 appleslices pineapplejuice_1.2_3.9 Mangojuice Mangojuice_5.55_5.55
输出应为:
orangejuice_988 pineapplejuice_120_390 appleslices_630_220 Mangojuice_555
从file1逐行读取时,搜索file2中file1行中找到的模式,找到后比较file2的第2和第3个字段,如果它们相同则打印一次,如果不打印这两个数字(无论如何,这个数字应乘以100)
我想到了这个:
while read -r -u 3 line1
do
nawk ' "$line1" print $0}' file2.txt
if "$2" == "$3"
then echo "scale=2;$2*100" |bc
else echo "$2_$3"
fi
done 3<file1.txt
所以,我想知道逻辑是否正确并修复了会给我988而不是988.0的乘法。
答案 0 :(得分:1)
您的逻辑更容易在Awk中纯粹表达。假设file1
的作用是将数据从file2
限制为仅限某些记录(您的示例似乎没有证明这一点),请尝试这样的事情。
awk -F _ '# Make OFS equal to FS
BEGIN { OFS=FS }
# Read file1 into a[]
NR==FNR { a[$0]++; next }
# If we fall through to here, we are in file2
# On any lines where $1 is in a[]
a[$1] {
if ($2==$3) print $1, $2*100;
else print $1, $2*100, $3*100;
}' file1 file2
答案 1 :(得分:1)
GNU awk
的一种方式:
$ awk 'NR==FNR{a[$0];next}($1 in a){print $1,$2==$3?$2*100:$2*100OFS$3*100}' FS=_ OFS=_ file1 file2
orangejuice_988
appleslices_630_220
pineapplejuice_120_390
Mangojuice_555