[英]Extract lines in file1 starting with a specific word given in file2
I have one file "file1" that contains lines like: 我有一个文件“ file1”,其中包含如下行:
643 2 3 4 5
6433 2 3 4 5
64 2 3 4 5
1234 2 3 4 5
1240 2 3 4 5
12 2 3 4 5
and I would like to extract from it all lines whose first word contained in file 2, which is like: 我想从中提取第一个单词包含在文件2中的所有行,例如:
12
64
Thus, the final result should be: 因此,最终结果应为:
12 2 3 4 5
64 2 3 4 5
In bash I think I have to use a loop for examining each word in file 2, but I do not know the command to extract the line in file1 containing the exact word. 在bash中,我认为我必须使用循环来检查文件2中的每个单词,但是我不知道用于提取文件1中包含确切单词的行的命令。
For example, using: 例如,使用:
sed -n '/^64/p' file1
I get: 我得到:
643 2 3 4 5 6433 2 3 4 5 64 2 3 4 5 643 2 3 4 5 6433 2 3 4 5 64 2 3 4 5
which is not correct, because I would like only line: 64 2 3 4 5 这是不正确的,因为我只想要一行:64 2 3 4 5
Do you know a bash method (sed, grep, awk, or python if you prefer) to do it? 您是否知道bash方法(如果愿意,可以使用sed,grep,awk或python)?
I'd say: 我会说:
awk 'NR == FNR { a[$1] = 1; next } a[$1]' file2 file1
That is: 那是:
NR == FNR { # while processing the first file (file2)
a[$1] = 1 # remember what values you saw
next # do nothing else
}
a[$1] # after that (while processing file1): print those whose first
# field was seen in the pass over file2.
You can use: 您可以使用:
awk 'NR==FNR{a[$1]; next} $1 in a' file2 file1
64 2 3 4 5
12 2 3 4 5
I think you can try with grep -w and more exactly: 我认为您可以尝试使用grep -w,更确切地说:
-w Searches for the expression as a word as if surrounded
by \< and \>.
so yu can try: 因此,您可以尝试:
grep -w 64 file1
Running on Solaris 10 在Solaris 10上运行
要使用grep,还要在搜索文件中的模式中添加锚点:
grep -wf <(sed 's/^/^/' file2) file1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.