【问题标题】:Compare two columns from two files and append the output in any of the two files as a new column比较两个文件中的两列,并将两个文件中的任何一个中的输出作为新列附加
【发布时间】:2020-11-20 17:15:49
【问题描述】:

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

【问题讨论】:

  • 感谢您在问题中分享您的努力,继续努力。您能否在您的问题中提及预期的示例输出以及获取它的逻辑(在您的问题中请不要在 cmets 中),以便更好地理解问题。
  • @RavinderSingh13-- 谢谢!提到了预期的输出,并且在我的代码中提到了逻辑。我只想匹配这两列 IF ( Filename== Filename && Col4 >Col 2) Then New_File Else Old_File
  • 当然,但是如果您在问题中以书面形式提及它(连同示例预期输出),并附上详细信息以便更好地理解,干杯。

标签: bash shell awk


【解决方案1】:

您能否尝试在 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.

【讨论】:

  • 还有一个问题——如果我还需要在我的最终输出中添加文件 1 中的第 1 列(Scorecard_1),我可以像 arr[$1] 一样使用它吗?另外,如果您能指出我学习 AWK 的任何链接,那就太好了。
  • @Amit,你可以试试arr[$2]=$1 OFS $NF,但现在最好不要更改解决方案,否则将来引用此线程的用户可能会感到困惑,如果有任何疑问,请告诉我。跨度>
  • -当然明白你的意思。我尝试了下面的方法来获得额外的一列,但没有得到想要的结果。 awk ' BEGIN{ FS=OFS="," } FNR==NR{ arr[$2]=$1 OFS $NF next } { print $1,arr[$2],(($1 in arr)?($NF>arr[ $1]?"Old_file":"New_File"):"No_Match") } ' file1.txt file2.txt
  • @Amit,您能否尝试关注,您可以玩转它,awk 'BEGIN{FS=OFS=","} FNR==NR{arr[$2]=$1 OFS $NF;arr2[$2]=$1;next} {print $1,(($1 in arr)?arr2[$1] OFS ($NF>arr[$1]?arr[$1] OFS "Old_file":"New_File"):"No_Match")}' file1 file2 不要将其添加到答案中,因为它不是第一个问题,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2016-06-23
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多