0

我有一个由 | 分隔的平面文件 我想从平面文件中已有的信息进行更新。我想使用第一个和第二个字段的信息填写第三个字段。从第一个字段开始,我想在使用该数据与缺少第三个字段的数据进行比较时忽略最后两个数字。当与第二个字段匹配时,我希望它是准确的。我不想创建新的平面文件。我想更新现有文件。我研究了一种从文件中提取前两个字段的方法,但我不知道这是否对我想要实现的目标有所帮助。总而言之,我想将第一个和第二个字段与文件中的其他字段进行比较,以提取平面文件某些行中可能缺少的第三个字段。

awk -F'|' -v OFS='|' '{sub(/[0-9 ]+$/,"",$1)}1 {print $1 "\t" $2}' tstfile

第一场|第二场|第三场

原始输入:

t1ttt01|/a1

t1ttt01|/b1

t1ttt01|/c1

t1ttt03|/a1|1

t1ttt03|/b1|1

t1ttt03|/c1|1

l1ttt03|/a1|3

l1ttt03|/b1|3

l1ttt03|/c1|3

它应该做什么:

t1ttt03|/a1|1 = t1ttt01|/a1

比较时t1ttt|/a1| = t1ttt|/a1

所以

t1ttt01|/a1变成 t1ttt01|/a1|/1

我希望输出看起来像什么:

t1ttt01|/a1|1

t1ttt01|/b1|1

t1ttt01|/c1|1

t1ttt03|/a1|1

t1ttt03|/b1|1

t1ttt03|/c1|1

l1ttt03|/a1|3

l1ttt03|/b1|3

l1ttt03|/c1|3
4

1 回答 1

0

一种方法awk

awk '

# set the input and output field separator to "|"

BEGIN{FS=OFS="|"}

# Do this action when number of fields on a line is 3 for first file only. The
# action is to strip the number portion from first field and store it as a key
# along with the second field. The value of this should be field 3

NR==FNR&&NF==3{sub(/[0-9]+$/,"",$1);a[$1$2]=$3;next} 

# For the second file if number of fields is 2, store the line in a variable
# called line. Validate if field 1 (without numbers) and 2 is present in
# our array. If so, print the line followed by "|" followed by value from array.

NF==2{line=$0;sub(/[0-9]+$/,"",$1);if($1$2 in a){print line OFS a[$1$2]};next}1
' file file

测试:

$ cat file
t1ttt01|/a1
t1ttt01|/b1
t1ttt01|/c1
t1ttt03|/a1|1
t1ttt03|/b1|1
t1ttt03|/c1|1
l1ttt03|/a1|3
l1ttt03|/b1|3
l1ttt03|/c1|3
$ awk 'BEGIN{FS=OFS="|"}NR==FNR&&NF==3{sub(/[0-9]+$/,"",$1);a[$1$2]=$3;next}NF==2{line=$0;sub(/[0-9]+$/,"",$1);if($1$2 in a){print line OFS a[$1$2]};next}1' file file
t1ttt01|/a1|1
t1ttt01|/b1|1
t1ttt01|/c1|1
t1ttt03|/a1|1
t1ttt03|/b1|1
t1ttt03|/c1|1
l1ttt03|/a1|3
l1ttt03|/b1|3
l1ttt03|/c1|3
于 2013-07-24T15:48:10.683 回答