我是unix的新手,并且要求合并两个固定长度的平面文件并且有共同点 记录是交易ID和批号。
在file1.txt中,交易ID从1到10,批次ID从19到21。 在file2.txt中,事务ID从12到21,批次ID从30到32。
对于两个文件中匹配的行/记录,从file2.txt剪切/复制发票号,然后将其追加到file1.txt中。发票号码与我的trans id不在同一行,&批号。
有人请帮忙。
*示例输入 - File1.txt *
161065730303122012098
161065846403042011784
161065916903012012431
161066813503042019678
161066835008092012136
161067040701122012984
161067106602122010267
*示例输入 - File2.txt *
2.60E5-2670161065730303122012098
userid 234
date03122012
Invnum987678
2.60E5-2670161065846403042011098
userid 871
date03122012
Invnum987912
2.60E5-2670161065916903012012075
userid 871
date031610671066122012
Invnum987654
2.60E5-2670161066813503042011075
userid 871
date03122012
Invnum987322
2.60E5-2670161066835008092012075
userid 871
date03122012
Invnum987323
2.60E5-2670161067040701122012075
userid 871
date03122012
Invnum987324
2.60E5-2670161067106602122010074
userid 811
date03122012
Invnum987325
所需的输出
161065730303122012098987678
161065846403042011784987912
161065916903012012431987654
161066813503042019678987322
161066835008092012136987323
161067040701122012984987324
161067106602122010267987325
答案 0 :(得分:1)
尝试这样的事情:
awk -F"-" '
NR==FNR && NF>1 {
v=substr($2,5,18);
next
}
NR==FNR && /Invnum/ {
sub(/Invnum/,"",$0);
a[v]=$0;
next
}
(substr($0,1,18) in a) {
print $0 a[substr($0,1,18)]
}' file2.txt file1.txt
<强>输出:强>
161065730303122012098987678
161065846403042011784987912
161065916903012012431987654
161066835008092012136987323
161067040701122012984987324
161067106602122010267987325
答案 1 :(得分:0)
我使用了文件a.txt和b.txt
for i in `cat b.txt`; do; a=$(echo $i|cut -c1-10); echo $(grep $a a.txt|cut -c12-)$(grep -A3 $a a.txt|tail -n1|cut -c7-) ;done
161065730303122012098987678
161065846403042011098987912
161065916903012012075987654
161066813503042011075987322
161066835008092012075987323
161067040701122012075987324
161067106602122010074987325
这样可以吗?
答案 2 :(得分:0)
awk 'FNR==NR {
if(length($0)==32)
p = substr($0,12,18)
if(/Invnum.*/)
A[p]=substr($0,7)
next
}
((s=substr($1,1,18)) in A){ print $1 A[s] }' file2 file1.txt
<强> - 编辑--- 强>
一个班轮
$ awk 'FNR==NR {if(length($0)==32)p = substr($0,12,18);if(/Invnum.*/)A[p]=substr($0,7);next}((s=substr($1,1,18)) in A){ print $1 A[s] }' file2 file1.txt