给定2个文件,两个文件都包含具有常量结构的行,例如:
档案A.
354guitar..06948banjo...05123ukulele.04
文件B.
354bass....04948banjo...04
我想在文件A上循环,并在文件B中搜索位置4-11中具有相同内容的行,但是在位置12-13中搜索不同的内容.
对于上面的情况,我希望文件B的第二行作为输出,具有“banjo …”匹配文件A和05的第二行!= 04.
我当时想用awk,但找不到自己的解决方案:(
谢谢!
解决方法 用awk真的很简单:$awk '{a=substr(#!/bin/awk -f{ # This is executed for every input line (both files) a=substr(,4,8);b=substr(,12,2)}NR==FNR{c[a]=b;next}a in c&&c[a]!=b' fileA fileB948banjo...04awk -f file.awk fileA fileB,8) # put characters 4 through 11 to variable a b=substr(,2) # put characters 12 and 13 to variable b}NR==FNR{ # This is executed only for the first file c[a]=b # store into map c index a,value b next # Go to the next record (remaining commands ignored)}# The remaining is only executed for the second file (due to the next command)(a in c) && (c[a] != b) # if a is an index of the map c,and the value # we prevIoUsly stored is not the same as the current b value # then print the current line (this is the default acttion)
或者以更易读的格式,您可以在脚本名称file.awk中保存以下内容
并执行如下:
总结以上是内存溢出为你收集整理的linux – 在一个文件中搜索另一个模式全部内容,希望文章能够帮你解决linux – 在一个文件中搜索另一个模式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)