簡體   English   中英

使用 awk 命令將 2 個文件與算術進行比較

[英]compare 2 files with arithmetic using awk command

我有2個文件:

文件 1

*Name|Date|id|Total*
Jimmy|03-OCT-18|BST100114262|20000
Dedi|03-OCT-18|BST100904288|10000

檔案 2

*Name|Amount*
Anton|9800
Jimmy|90000

輸出:吉米|20000|90000|1800000000

我試過了,但沒有運氣。

awk -F'|' 'NR==FNR{a[$1]=$1; next} (($1 in a) && (a[$4] >= $2 )) { print a[$4]*$2 }'

您可以嘗試以下嗎?

awk -F'|' 'NR==FNR{a[$1]=$4; next} (($1 in a) && (a[$1] <= $2 )) {$(NF+1)=a[$1]*$2;$2=a[$1] OFS $2;print}'  OFS="|" File1 File2

輸出如下。

Jimmy|20000|90000|1800000000

另一個:

$ 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

使用Jimmies進行了一些解釋:

$ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM