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



我对 shell 脚本非常陌生,并找到了一种在使用md5sum时如何使用 shell 脚本比较文件的方法。

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

例如,如果我们比较Options_oldOptions_new文件并检查Options_new文件,则510051 2 C2.50新的股票代码字段值并添加了510052 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 $1}'`
MD5SUM_OLD=`md5sum ${NEW_PATH}/${FILE} | awk '{print $1}'`
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}

也许值得深思 运行以检查是否不同,如果是,则打印包含您指示要保存到 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 "$2" "$1" | 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 '$1 ~ /^ *>/' | awk '{print $2","$5","$7 }')
#same again only for new vars
addedSyn=$(diff file2 file1 | awk '$1 ~ /^ *</' | awk '{print $2","$5","$7 }')
echo "$changeSyn"
echo "$addedSyn"
else
echo "No change"
fi

尝试使用融合进行视觉效果

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

最新更新