提取 file1 中以 file2 中给定的特定单词开头的行
Extract lines in file1 starting with a specific word given in file2
我有一个文件 "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
我想从中提取第一个单词包含在文件 2 中的所有行,例如:
12
64
因此,最终结果应该是:
12 2 3 4 5
64 2 3 4 5
在 bash 中,我想我必须使用循环来检查文件 2 中的每个单词,但我不知道用于提取文件 1 中包含确切单词的行的命令。
例如,使用:
sed -n '/^64/p' file1
我得到:
643 2 3 4 5
6433 2 3 4 5
64 2 3 4 5
这是不正确的,因为我只想要一行:
64 2 3 4 5
您知道 bash 方法(sed、grep、awk 或 python,如果您愿意)吗?
我会说:
awk 'NR == FNR { a[] = 1; next } a[]' file2 file1
即:
NR == FNR { # while processing the first file (file2)
a[] = 1 # remember what values you saw
next # do nothing else
}
a[] # after that (while processing file1): print those whose first
# field was seen in the pass over file2.
您可以使用:
awk 'NR==FNR{a[]; next} in a' file2 file1
64 2 3 4 5
12 2 3 4 5
我认为您可以尝试使用 grep -w 更准确地说:
-w Searches for the expression as a word as if surrounded
by \< and \>.
所以你可以试试:
grep -w 64 file1
运行 在 Solaris 10
要使用 grep,还要在搜索文件中的模式中添加一个锚点:
grep -wf <(sed 's/^/^/' file2) file1
我有一个文件 "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
我想从中提取第一个单词包含在文件 2 中的所有行,例如:
12
64
因此,最终结果应该是:
12 2 3 4 5
64 2 3 4 5
在 bash 中,我想我必须使用循环来检查文件 2 中的每个单词,但我不知道用于提取文件 1 中包含确切单词的行的命令。
例如,使用:
sed -n '/^64/p' file1
我得到:
643 2 3 4 5 6433 2 3 4 5 64 2 3 4 5
这是不正确的,因为我只想要一行: 64 2 3 4 5
您知道 bash 方法(sed、grep、awk 或 python,如果您愿意)吗?
我会说:
awk 'NR == FNR { a[] = 1; next } a[]' file2 file1
即:
NR == FNR { # while processing the first file (file2)
a[] = 1 # remember what values you saw
next # do nothing else
}
a[] # after that (while processing file1): print those whose first
# field was seen in the pass over file2.
您可以使用:
awk 'NR==FNR{a[]; next} in a' file2 file1
64 2 3 4 5
12 2 3 4 5
我认为您可以尝试使用 grep -w 更准确地说:
-w Searches for the expression as a word as if surrounded
by \< and \>.
所以你可以试试:
grep -w 64 file1
运行 在 Solaris 10
要使用 grep,还要在搜索文件中的模式中添加一个锚点:
grep -wf <(sed 's/^/^/' file2) file1