将 shell 脚本中的文件与 md5sum 进行比较并为更改后的文件创建 csv

compare files in shell script with md5sum and create csv for the changed file

我对 shell 脚本非常陌生,并且找到了一种在使用 md5sum.

的同时使用 shell 脚本比较文件的方法

我想比较 shell 脚本中的 Options_oldOptions_new 文件,并确定新文件中添加的新 Ticker 字段值。对于这个新的代码字段值,我想创建 CSV 文件。

例如,如果我们比较 Options_oldOptions_new 文件并检查 Options_new 文件,则会添加新的代码字段值 510051 2 C2.50510052 2 P2.50 并且我想在 CSV 文件中创建并打印此值。

Options_new.out.gz 文件

START-OF-FILE
PROGRAMNAME=getdata
DATEFORMAT=yyyymmdd
START-OF-FIELDS
TICKER
EXCH_CODE
END-OF-FIELDS
TIMESTARTED=Wed Feb 12 19:30:38 JST 2020
START-OF-DATA
510051 CH 02/26/20 C2.5 Equity|0|75|510051 2 C2.50|CH
510052 CH 02/26/20 P2.5 Equity|0|75|510052 2 P2.50|CH
510050 CH 02/26/20 C2.55 Equity|0|75|510050 2 C2.55|CH
510050 CH 02/26/20 P2.55 Equity|0|75|510050 2 P2.55|CH
END-OF-DATA
DATARECORDS=1140
TIMEFINISHED=Wed Feb 12 19:32:50 JST 2020
END-OF-FILE

Options_old.out.gz 文件

START-OF-FILE
PROGRAMNAME=getdata
DATEFORMAT=yyyymmdd
START-OF-FIELDS
TICKER
EXCH_CODE
END-OF-FIELDS
TIMESTARTED=Wed Feb 12 19:30:38 JST 2020
START-OF-DATA
510050 CH 02/26/20 C2.5 Equity|0|75|510050 2 C2.50|CH
510050 CH 02/26/20 P2.5 Equity|0|75|510050 2 P2.50|CH
510050 CH 02/26/20 C2.55 Equity|0|75|510050 2 C2.55|CH
510050 CH 02/26/20 P2.55 Equity|0|75|510050 2 P2.55|CH
END-OF-DATA
DATARECORDS=1140
TIMEFINISHED=Wed Feb 12 19:32:50 JST 2020
END-OF-FILE

我已经开始编写代码,但还没有进一步了解如何比较特定字段然后生成 csv 文件:

#!/bin/sh

OLD_PATH="/opt/old"
NEW_PATH="/opt/new"

FILES="${FILES} Options_new.out.gz Options_old.out.gz"

for FILE in `echo ${FILES}`
do
   MD5SUM_NEW=`md5sum ${OLD_PATH}/${FILE} | awk '{print }'`
   MD5SUM_OLD=`md5sum ${NEW_PATH}/${FILE} | awk '{print }'`

   if [ "${MD5SUM_NEW}" != "${MD5SUM_OLD}" ]; then
      echo "Found new Version of ${FILE}"
#currently i am comparing the data from the whole file but i want to compare the data only for the Ticker value in the both files

#here create new csv file with the new ticker value found in Options_new.out.gz file

   fi

exit ${EXIT}

尝试使用视觉融合

 meld file1 file2

命令行差异

 diff file1 file2
 10,11c10,11
 < 510051 CH 02/26/20 C2.5 Equity|0|75|510051 2 C2.50|CH
 < 510052 CH 02/26/20 P2.5 Equity|0|75|510052 2 P2.50|CH
 ---
 > 510050 CH 02/26/20 C2.5 Equity|0|75|510050 2 C2.50|CH
 > 510050 CH 02/26/20 P2.5 Equity|0|75|510050 2 P2.50|CH

也许值得深思 运行以检查是否不同,如果不同,则打印包含您表示希望保存到 csv

的位的行
#!/bin/bash

#Check if file are different then grep for word differ 
#normally would spit out Files file2 and file1 differ
# flags are -F fixed string, -w match only full words
# -q quiet ie no output to stdout (screen)

if $(diff -q "" "" | grep -Fwq "differ")
then
    #create a var of the changed text, awk looking at 
    #begining of line to see if begins with > and then
    #output the full fine for awk to then select the 
    #vars you want
    changeSyn=$(diff file2 file1 | awk ' ~ /^ *>/' | awk '{print ",""," }')
    #same again only for new vars
    addedSyn=$(diff file2 file1 | awk ' ~ /^ *</' | awk '{print ",""," }')
    echo "$changeSyn"
    echo "$addedSyn"
else
    echo "No change"
fi