[英]awk find and replace variable file2 into file1 when matched
Tried couple of answers from similar questions but not quite getting correct results. 尝试了类似问题的答案,但并没有获得正确的结果。 Trying to search second file for variable and replace with second variable if there, otherwise keep original...
尝试在第二个文件中搜索变量,如果有则替换为第二个变量,否则保留原始文件...
File1.txt File1.txt
a
2
c
4
e
f
File2.txt File2.txt
2 b
4 d
Wanted Output.txt 想要的Output.txt
a
b
c
d
e
f
So far what I have seems to sort of work, but anywhere the replacement is happening I'm getting a blank row instead of the new variable... Current Output.txt 到目前为止,我似乎可以进行一些工作,但是在发生替换的任何地方,我都会得到空白行而不是新变量... Current Output.txt
a
c
e
f
Curent code.... 当前代码...
awk -F'\t' 'NR==FNR{a[$1]=$2;next} {print (($1 in a) ? a[$1] : $1)}' file2.txt file1.txt > output.txt
Also tried and got same results... 也尝试过并得到相同的结果...
awk -F'\t' 'NR==FNR{a[$1]=$2;next} {$1 = a[$1]}1' file2.txt file1.txt > output.txt
Sorry first wrote incorrectly..fixed the key value issue. 对不起,首先写错了。.解决了键值问题。
Did try what you did, still not getting missing in output.txt 尝试过您所做的事情,但仍然不会在output.txt中丢失
awk -F'\t' 'NR==FNR{a[$1]=$2;next} $1 in a{$1 = a[$1]}1' file2.txt file1.txt > output.txt
your key value pair is not right... $1 is the key, $2 is the value. 您的键值对不正确... $ 1是键,$ 2是值。
$ awk -F'\t' 'NR==FNR{a[$1]=$2;next} $1 in a{$1=a[$1]}1' file.2 file.1
a
b
c
d
e
f
try below solution - 尝试以下解决方案-
awk 'NR==FNR{a[$1]=$NF;next} {print (a[$NF]?a[$NF]:$1)}' file2.txt file1.txt
a
b
c
d
e
f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.