我正在尝试以这种方式比较两个不同的文件,比如说“ file1”和“ file2”。如果两个文件(文件1和文件2)中字符的子字符串(即位置8到12的5个字符)都匹配,则从文件1中删除该匹配行。最后,将输出写入文件3。(输出包含不包含的其余行)与文件2匹配)我的输出是文件1的不匹配行。输出(文件3)=文件1-文件2
File1
-----
aqcdfdf**45555**78782121
axcdfdf**45555**75782321
aecdfdf**75555**78782221
aqcdfdf**95555**78782121
File2
-----
aqcdfdf**45555**78782121
axcdfdf**25555**75782321
File3
-----
aecdfdf**75555**78782221
aqcdfdf**95555**78782121
我尝试了awk,但是我需要一些东西来查看两个文件的子字符串,因为我的文件中没有分隔符。$ awk'FNR == NR {a [$ 1]; 下一个} $ 1 in a'f1 f2> file3
您可以尝试按照GNU中显示的示例进行尝试,编写和测试awk
。一旦对终端的结果感到满意,然后将以下命令的输出重定向到 > file3
(追加 > file3
到以下命令)。
awk '{str=substr($0,8,5)} FNR==NR{a[str];next} !(str in a)' file2 file1
说明:添加了以上的详细说明。
awk ' ##Starting awk program from here.
{
str=substr($0,8,5) ##Creating str which has sub-string of current line from 8th to 12th character.
}
FNR==NR{ ##Checking condition FNR==NR which will run when Input_file2 is being read.
a[str] ##Creating array a with index of str here.
next ##next will skip all further statements from here.
}
!(str in a) ##Checking condition if str is NOT present in a then print that line from Input_file1.
' file2 file1 ##Mentioning Input_file names here.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句