我想在Linux命令行上搜索一个字符串(除非在文件中查找,否则我不知道)。
示例:
我想在文件中回显这个词(两个文件中都存在)或将其存储在变量中。
这怎么可能?
答案 0 :(得分:2)
您可以使用以下命令获取文件中所有唯一单词的列表:
grep -o -E '\w+' filename | sort -u
其中-E '\w+'
是匹配的单词,而-o
输出匹配的部分。然后,我们可以使用join
命令来识别两个文件中的匹配行,并使用进程替换来传递单词查找器的结果:
join <(grep -o -E '\w+' filename1 | sort -u) <(grep -o -E '\w+' filename2 | sort -u)
答案 1 :(得分:0)
如果没有重复,则将单个文件变白,您可以使用cat file1 file2 |sort | uniq -d
答案 2 :(得分:0)
$ cat input_one.txt
FIREFOX a
FIREFOX b
Firefox a
firefox b
$ cat input_two.txt
CHROME a
FIREFOX a
EXPLORER a
$ while read line; do grep "$line" input_two.txt ; done < input_one.txt
FIREFOX a
Explanation:
while
will loop every line with input_two.txt
file as input and will store the temporary line in the line
variable.
In every line will search for it in the input_one.txt
file and -o
option will make to print only the matched part.
EDIT: See comments
答案 3 :(得分:-1)
您可以编写脚本来处理此问题。 您需要在文件1上循环一个单词,在循环中使用grep命令(grep -nwr“” -e“ $ word”)在文件2中找到一个单词。 如果匹配,请回声。