繁体   English   中英

比较两个文件中的两列,并将这两个文件中的任何一个中的输出作为新列附加

[英]Compare two columns from two files and append the output in any of the two files as a new column

.csv 文件 1:

Scorecard_1,ZDTJ_PREV.EXT,12
Scorecard_2,ZACN_PREV.EXT,6
Scorecard_3,ABC.txt,8

文本文件 2:

Acct_Bal_Non_Zero_Tgt.txt,7243
IDQ_HB1.txt,5380
IDQ_HB_LOGC.txt,5380
ZACN_PREV.EXT,4
ZDTJ_PREV.EXT,3
ABC.txt,10

输出:

Acct_Bal_Non_Zero_Tgt.txt,No_Match
    IDQ_HB1.txt,No_Match
    IDQ_HB_LOGC.txt,No_Match
    ZACN_PREV.EXT,New_File
    ZDTJ_PREV.EXT,New_File
    ABC.txt,Old_File

逻辑:如果第二个文件中存在匹配的文件,则比较两个文件的最后一列。 如果文件一中的最后一列大于第二个文件的最后一列,则该文件是新的,否则该文件是旧的。

我的任务是根据间隔(这是第一个文件的最后一列)确定文件是当天还是前一天。我的方法非常简单,我知道通过 awk 有更简单的方法。

到目前为止,我已尝试以下:

File_in_CSV=$(cat file_1.csv | awk -F "," '{ print $3 }' | tail --lines=+1 | sort -u | uniq )
File_in_age_file=$(cat File_2.txt | awk -F "," '{ print $1 }' | tail --lines=+1 | sort -u | uniq )
Age=$(cat All_Files_Age.txt | awk -F "," '{ print $2 }' | tail --lines=+1 | sort -u | uniq )
Interval=$(cat scorecard_file_details.csv | awk -F "," '{ print $4 }' | tail --lines=+1 | sort -u | uniq )
for file in $File_in_CSV; do
if [[ "$file" = $File_in_age_file && $Age>$Interval ]]; then
printf "%s\n" "File is of Previous Day!"
else
printf "%s\n" "File is of Current Day!"
fi
done

我还希望将此前一天或当天的标志作为任何文件中的一列附加。 感谢您对此的帮助!!

预期输出的示例是:

FileName, Flag
ABC.txt, New_File/Old_File

您能否尝试在 GNU awk使用显示的示例进行跟踪、编写和测试。

awk '
BEGIN{
  FS=OFS=","
}
FNR==NR{
  arr[$2]=$NF
  next
}
{
  print $1,(($1 in arr)?($NF>arr[$1]?"Old_file":"New_File"):"No_Match")
}
'  Input_file1  Input_file2

说明:为以上添加详细说明。

awk '                           ##Starting awk program from here.
BEGIN{                          ##Starting BEGIN section of this program from here.
  FS=OFS=","                    ##Setting field separator and outpout field separator as comma.
}
FNR==NR{                        ##Checking condition if FNR==NR when Input_file1 is being read.
  arr[$2]=$NF                   ##Creating arr with index of 2nd field and having value as last field.
  next                          ##next will skip further statements from here.
}
{
  print $1,($1 in arr)?($NF>arr[$1]?"Old_file":"New_File"):"No_Match"
                                ##printing 1st field and checking condition if 1st field is present in arr then check
                                ##if last field is greater than arr value then print Old_file else print
                                ##New_file OR if 1st field is NOT in arr then print No_Match.
}
'  Input_file1  Input_file2     ##Mentioning Input_file names here.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM