我正在编写我的初始bash脚本,在一个需要论坛帮助的地方卡住了。
如何实现下面的shell脚本?(任何建议/指针都很感激!!)
要求:
比较2个文件匹配KEY包含长字符串,并且只在第三个文件中保存其他属性不同的长字符串(例如USER的值不同)。也跳过一些属性比较。
输入FILE1——AAUTOX=Y;ACCT=;ACTION=C;APRICE=99.975;AQTY=5541;USER=Sam,bpl;CONFIRM=Y;KEY=29976DYE4;DEPT=MYNA-CLCD -- same
AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Todd,chr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD -- diff (USER=Todd,chr) write in result file
输入FILE2 AAUTOX=Y;ACCT=;ACTION=C;APRICE=99.975;AQTY=5541;USER=Sam,bpl;CONFIRM=Y;KEY=29976DYE4;DEPT=MYNA-CLCD -- same
AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Alan,ncr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD -- diff (USER=Alan,ncr) write in result file
AAUTOX=Y;ACCT=;ACTION=C;APRICE=17.000;AQTY=6453;USER=Todd,chr;CONFIRM=N;KEY=29976DYE6;DEPT=MYNA-CLCD -- no match (KEY) found write in result file
输出FILE3: FILE1:AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Todd,chr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD
FILE2:AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Alan,ncr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD
FILE1:
FILE2: AAUTOX=Y;ACCT=;ACTION=C;APRICE=17.000;AQTY=6453;USER=Todd,chr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD
,依此类推....
我心目中的方法(这是第一次切割,以后可以改进):
- 逐行读取FILE1 (awk或Read ??)每一行
- a)读取FILE2以匹配唯一的"KEY"(这里使用哪个命令??awk可以根据键读取文件吗??grep键从FILE2,但如何断行到字段进行比较??)
- b)现在比较FILE1的每个字段。LINE1与FILE2。行,如果不一样,写在第三个结果文件中(awk将行分成$1,$2字段,这样可以比较虽然不确定如何做,如果使用"读取"命令??)
这使用GNU awk 4。*对于排序的in
(参见http://www.gnu.org/software/gawk/manual/gawk.html#Controlling-Array-Traversal),您可以使用其他awks来排序或以其他方式确定键顺序:
$ cat tst.awk
BEGIN { FS="[;=]" }
{
delete name2val
for (i=1; i<=NF; i+=2) { name2val[$i] = $(i+1) }
key = name2val["KEY"]
keys[key]
recs[key,FILENAME] = $0
for (name in name2val) { vals[key,FILENAME,name] = name2val[name] }
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
file1 = ARGV[1]
file2 = ARGV[2]
for (key in keys) {
state = "SAME"
if ( (key,file1) in recs ) {
if ( (key,file2) in recs ) {
for (name in name2val) {
if (name != "CONFIRM") {
if (vals[key,file1,name] != vals[key,file2,name]) {
state = "DIFF"
}
}
}
} else { state = "FILE1_ONLY" }
} else { state = "FILE2_ONLY" }
if (state != "SAME") {
print file1":", recs[key,file1]
print file2":", recs[key,file2]
print ""
}
}
}
.
$ gawk -f tst.awk FILE1 FILE2
FILE1: AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Todd,chr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD -- diff (USER=Todd,c
hr) write in result file
FILE2: AAUTOX=Y;ACCT=;ACTION=C;APRICE=05.975;AQTY=3451;USER=Alan,ncr;CONFIRM=N;KEY=29976DYE5;DEPT=MYNA-CLCD -- diff (USER=Alan,ncr) write in result file
FILE1:
FILE2: AAUTOX=Y;ACCT=;ACTION=C;APRICE=17.000;AQTY=6453;USER=Todd,chr;CONFIRM=N;KEY=29976DYE6;DEPT=MYNA-CLCD -- no match (KEY) found write in result file