如何使用sed里面awk的空间 [英] How to use Sed with spaces inside awk
问题描述
我的问题是:
我已经file1中:
My problem is : I have file1 :
3119
3359
1359
...
和文件2
2222 abc
3359 def <---- this is a match
4444 ghi
...
和我想修改其中来自文件1索引文件由2 prefixing字符串中找到的每个线。结果将是:
And I would like to modify each lines where indexes from file 1 are found in file 2 by prefixing the string. Result would be :
2222 abc
3359 PREFIXdef <---- this is a match
4444 ghi
...
我想出了这个解决方案:
I came up with this solution:
sed `cat file1.txt | awk '{print "-e s/"$1"/"$1"PREFIX./"}'`<<<"`cat file2.txt`"
这给这样的结果:
Which give this result :
2222 abc
3359PREFIX def <---- this is a match
4444 ghi
...
我如何添加preFIX前一个空格,后删除吗?我有一个错误
(命令没有终止)
How can I add a space before PREFIX and remove the one after ? I have an error (command not terminated)
在理想情况下我想补充约2撇号的/ XX / XX:
Ideally I would add 2 apostrophes around the 's/xx / xx':
sed `cat file1.txt | awk '{print "-e 's/"$1" /"$1" PREFIX./'"}'`<<<"`cat file2.txt`"
但我不能逃脱''!
But I can't escape the '' !!!
推荐答案
这awk命令可以达到从第一个文件中的所有列1到一个关联数组,然后检查处理的,针对文件2:
This awk command can handle that by reaching all 1st columns from first file into an associative array and then checking that against file2:
awk 'FNR==NR{a[$1]; next} $1 in a{$2 = "PREFIX" $2} 1' file1 file2
2222 abc
3359 PREFIXdef
4444 ghi
这篇关于如何使用sed里面awk的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!