我有两个文件:
1.txt
/stackoverflow.com/questions
/stackoverflow.com/reports
/stackoverflow.com/answers
2.txt
/stackoverflow.com/questions/18959800/grep-each-line-from-a-file-in-another-file
/stackoverflow.com/answers/18984800/grep-each-line-from
/stackoverflow.com/questions/20559800/line-from-a-file-in-another-file
/stackoverflow.com/ask/9959800/file-in-another-file-second_line
如果来自1.txt的行是来自2.txt的行的一部分,请打印此行。如果2.txt中的一行不匹配,请打印此行。
我想要这样的output.txt文件:
output.txt:
/stackoverflow.com/questions/
/stackoverflow.com/reports/
/stackoverflow.com/answers/
/stackoverflow.com/ask/9959800/file-in-another-file-second_line
可以将所有行放在一个文件中:
/stackoverflow.com/questions
/stackoverflow.com/reports
/stackoverflow.com/answers
/stackoverflow.com/questions/18959800/grep-each-line-from-a-file-in-another-file
/stackoverflow.com/answers/18984800/grep-each-line-from
/stackoverflow.com/questions/20559800/line-from-a-file-in-another-file
/stackoverflow.com/ask/9959800/file-in-another-file-second_line
编辑:由于OP已添加了来自先前Input_file的行,因此也需要编辑解决方案,如下所示。
awk 'FNR==NR{a[$0]=$0;next} {for(i in a){if(match($0,a[i])){print substr($0,RSTART,RLENGTH);b[i]=i;next}};print;} END{for(k in a){if(b[k]==""){print a[k]}}}' 1.txt 2.txt
现在也添加非单一衬里形式的解决方案。
awk '
FNR==NR{
a[$0]=$0;
next}
{
for(i in a){
if(match($0,a[i])){
print substr($0,RSTART,RLENGTH);
b[i]=i;
next}};
print}
END{
for(k in a){
if(b[k]==""){
print a[k]}}
}
' 1.txt 2.txt
您能否尝试遵循并让我知道这是否对您有帮助。
awk 'FNR==NR{a[$0]=$0;next} {for(i in a){if(match($0,a[i])){print substr($0,RSTART,RLENGTH);next}};print}' 1.txt 2.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句