如何在linux中搜索具有空格的模式的文件

时间:2022-01-14 08:53:48

I am struggling hence need your help ( in linux ).

我很挣扎因此需要你的帮助(在linux中)。

a) I have a file with two columns separated by a white space ( delimiter is " "). Infact, I run series of piped command on the command line which gives me output as mentioned above.

a)我有一个文件,其中两列用空格分隔(分隔符为“”)。事实上,我在命令行上运行一系列管道命令,它给出了如上所述的输出。

aaa bbb
ccc ddd
fff ggg
ccc nnn
fff kkk    # there are approx 20,000 such rows.

b) I have lot of others file such as file-1.txt, file-2. txt , file-3.txt.

b)我有很多其他文件,如file-1.txt,file-2。 txt,file-3.txt。

Problem: I need to search for each line in the output mentioned in section a. above. To illustrate, I want to run equivalent of:

问题:我需要在a节中提到的输出中搜索每一行。以上。为了说明,我想运行相当于:

grep 'aaa bbb' file-1 txt file-2.txt file-3 txt 
grep 'ccc ddd' file-1 txt file-2.txt file-3 txt
......
20,000 times
.......

But above command takes hell lot of time.

但是上面的命令花了很多时间。

Question:

How do I use a single series of command to perform this operation. Whenever I am running command ( as shown below), system only searches for individual words in the line i.e. for aaa and bbb separately and gives me wrong output.

如何使用单个命令系列来执行此操作。每当我运行命令时(如下所示),系统只搜索行中的单个单词,即分别搜索aaa和bbb,并给出错误的输出。

eg:

cat < filename > | cut -d "," -f1,2 | xargs -I {} sed '{}' file-1.txt

or using grep instead of sed....

或使用grep而不是sed ....

NOTE: command before pipe is bringing the output in space as mentioned in the point a. above.

注意:管道之前的命令将输出带入空间,如a点所述。以上。

Any help will be much appreciated.

任何帮助都感激不尽。

2 个解决方案

#1


Store all the patterns (aaa bbb etc.) to a file (patterns.txt), one per line, and then

将所有模式(aaa bbb等)存储到文件(patterns.txt),每行一个,然后

grep -f patterns.txt file-*.txt

will do the job.

会做的。

#2


Expanding upon Hin's answer, you can produce the patterns and do the grep search in one line using bash's process substitution:

扩展Hin的答案,您可以使用bash的进程替换生成模式并在一行中执行grep搜索:

grep -F -f <(cut -d, -f1,2 filename) file-*.txt

I'm assuming the patterns you're producing are fixed strings, not regular expressions, hence the -F option

我假设你生成的模式是固定字符串,而不是正则表达式,因此-F选项

#1


Store all the patterns (aaa bbb etc.) to a file (patterns.txt), one per line, and then

将所有模式(aaa bbb等)存储到文件(patterns.txt),每行一个,然后

grep -f patterns.txt file-*.txt

will do the job.

会做的。

#2


Expanding upon Hin's answer, you can produce the patterns and do the grep search in one line using bash's process substitution:

扩展Hin的答案,您可以使用bash的进程替换生成模式并在一行中执行grep搜索:

grep -F -f <(cut -d, -f1,2 filename) file-*.txt

I'm assuming the patterns you're producing are fixed strings, not regular expressions, hence the -F option

我假设你生成的模式是固定字符串,而不是正则表达式,因此-F选项