我有两个文件:
文件1
*Name|Date|id|Total*
Jimmy|03-OCT-18|BST100114262|20000
Dedi|03-OCT-18|BST100904288|10000
文件2
*Name|Amount*
Anton|9800
Jimmy|90000
输出:Jimmy | 20000 | 90000 | 18000000
我试过了,但没有成功。
awk -F'|' 'NR==FNR{a[$1]=$1; next} (($1 in a) && (a[$4] >= $2 )) { print a[$4]*$2 }'
另一个:
$ awk 'BEGIN{FS=OFS="|"}($1 in a)&&FNR>1{print $1,a[$1],$NF,a[$1]* $NF}{a[$1]=$NF}' file1 file2
输出:
Jimmy|20000|90000|1800000000
解释了一些使用吉米:
$ awk '
BEGIN { FS=OFS="|" } # set separators
($1 in a) && FNR>1 { # if key to hash (Jimmy) is in a excluding headers
print $1,a[$1],$NF,a[$1]* $NF # process Jimmy
# next # this excludes new Jimmy from being rehashed to a
}
{
a[$1]=$NF # ... in here where everything is hashed and the
}' file1 file2 # ... existing rehashed