Bash 如何在file1.txt和file2.txt中搜索匹配字符并将输出打印到新文件
我遇到了一个问题!我只是没有足够的知识来独自解决这个问题,所以如果有人能帮助我,我将不胜感激 我有两个文本文件:Bash 如何在file1.txt和file2.txt中搜索匹配字符并将输出打印到新文件,bash,awk,grep,formatting,text-processing,Bash,Awk,Grep,Formatting,Text Processing,我遇到了一个问题!我只是没有足够的知识来独自解决这个问题,所以如果有人能帮助我,我将不胜感激 我有两个文本文件:file1.txt和file2.txt;它们具有相似的格式,但不精确。名称位于单独的行号上,文件具有不同的行号。由于文件中的数据量,手动执行此操作是不可行的 文件格式示例: file1.txt NAME:FLAT Jerome:Flat 6 Jimmy:Flat 4 file2.txt 0:NAME:JOB:MONEY:FLAT 1:Bob:Developer:$500:Flat 7
file1.txt
和file2.txt
;它们具有相似的格式,但不精确。名称位于单独的行号上,文件具有不同的行号。由于文件中的数据量,手动执行此操作是不可行的
文件格式示例:
file1.txt
NAME:FLAT
Jerome:Flat 6
Jimmy:Flat 4
file2.txt
0:NAME:JOB:MONEY:FLAT
1:Bob:Developer:$500:Flat 7
2:Jerome:Gardener:$50:Flat 6
3:Cindy:Graphics:$100:Flat 5
4:Jimmy:Mod:$150:Flat 4
我正在尝试搜索file1.txt,以查看哪个名称与file2的名称匹配,并将file2.txt的整行打印到一个新的文本文档中
下面是我想做的一个例子:
Checks matching NAME in file1.txt and file2.txt
Ignores "1:Bob:Developer:$500:Flat 7" because Bob only exists in file2.txt
Pastes "2:Jerome:Gardener:$50:Flat 6" into file3.txt because Jerome exists in file1.txt and file2.txt
Ignores "3:Cindy:Graphics:$100:Flat 5" because Cindy only exists in file2.txt
Pastes "4:Jimmy:Mod:$150:Flat 4" into file3.txt because Jimmy exists in file1.txt and file2.txt
文件3看起来怎么样
File3.txt
2:Jerome:Gardener:$50:Flat 6
4:Jimmy:Mod:$150:Flat 4
谢谢你的阅读!如果有人能告诉我这是否可能,那就太好了
编辑:
到目前为止我所拥有的
awk -F ":" 'FNR==NR{a[$1];next}($1 in a){print}' file2.txt file1.txt > file3.txt
使用一些GNU工具:
join -t ":" -1 1 -2 2 <(sed 1d File1.txt | sort) <(sort -t ":" -k 2,2 File2.txt) -o 2.1,2.2,2.3,2.4,2.5
join-t:“-11-22使用一些GNU工具:
join -t ":" -1 1 -2 2 <(sed 1d File1.txt | sort) <(sort -t ":" -k 2,2 File2.txt) -o 2.1,2.2,2.3,2.4,2.5
join-t:“-11-22对于您展示的示例,您可以尝试以下内容吗。使用GNUawk
编写和测试
awk '
BEGIN { FS=":" }
FNR==1 { next }
FNR==NR{
arr[$1]
next
}
($2 in arr)
' file1.txt file2.txt
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN { FS=":" } ##Starting BEGIN section from here and setting FS as : here.
FNR==1 { next } ##Checking if this is first line in any of Input_file then simply go to next line.
FNR==NR{ ##This condition will be TRUE when file1.txt is being read.
arr[$1] ##Creating array with $1 as key here.
next ##next will skip all further statements from here.
}
($2 in arr) ##Checking condition if 2nd fueld is in arr then print line from file2.txt
' file1.txt file2.txt ##Mentioning Input_file names here.
有了你们展示的样品,你们能试一下下面的吗。使用GNUawk
编写和测试
awk '
BEGIN { FS=":" }
FNR==1 { next }
FNR==NR{
arr[$1]
next
}
($2 in arr)
' file1.txt file2.txt
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN { FS=":" } ##Starting BEGIN section from here and setting FS as : here.
FNR==1 { next } ##Checking if this is first line in any of Input_file then simply go to next line.
FNR==NR{ ##This condition will be TRUE when file1.txt is being read.
arr[$1] ##Creating array with $1 as key here.
next ##next will skip all further statements from here.
}
($2 in arr) ##Checking condition if 2nd fueld is in arr then print line from file2.txt
' file1.txt file2.txt ##Mentioning Input_file names here.
使用-F:
作为字段分隔符,您还需要在a
中选中$2,文件顺序应为file1 file2。。。请参阅以获取解释我找到了一个暂时可行的解决方法,谢谢您的帮助。这不完全是我需要的,但现在可以了!我不会在这里发布它,因为它不是真正相关的:)使用-F:
作为字段分隔符,并且您需要在一个中选中$2,文件的顺序应该是file1 file2。。。请参阅以获取解释我找到了一个暂时可行的解决方法,谢谢您的帮助。这不完全是我需要的,但现在可以了!我不会把它贴在这里,因为它不是真正相关的:)