bash 逐行读取文件 -> 搜索另一个文本文档中的每一行,如果匹配,则输出到另一个文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5284785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:36:28  来源:igfitidea点击:

Read a file line by line -> search for each line in another text document, if match, output to another text file

linuxbashshell

提问by eveo

I have a script that takes two parameters.

我有一个需要两个参数的脚本。

ls  > .txt
ls  > .txt  

I now have a.txt ($1 is directory 'a', $2 is directory 'b') and b.txt each with their contents listed inside them. What I want to do is search line by line in a.txt and see if there is a match in b.txt.

我现在有 a.txt($1 是目录 'a',$2 是目录 'b')和 b.txt,其中每个都列出了它们的内容。我想要做的是在a.txt中逐行搜索,看看b.txt中是否有匹配项。

If I have these files in a.txt:

如果我在 a.txt 中有这些文件:

file1 -> search for file1 in b.txt, if match, output to a_match.txt
file2 -> search for file2 in b.txt, if match, output to a_match.txt
file3 -> search for file3 in b.txt, if match, output to a_match.txt  

Vice versa for b.txt. How would I do this? Also, not looking for alternative methods to doing this, there are soooo many other ways to check differences amongst directories, but this is the only acceptable way for my assignment :(

b.txt 反之亦然。我该怎么做?此外,不是在寻找替代方法来执行此操作,还有很多其他方法可以检查目录之间的差异,但这是我分配的唯一可接受的方法:(

采纳答案by Charlie Martin

Okay, this is clearly homework so I'm not going to give you a full solution. Here's the outline

好的,这显然是家庭作业,所以我不会给你一个完整的解决方案。这是大纲

while "there are lines left in the file, read a line" do
     grep "the line" "the file"
done

the key for getting lines is the readcommand. Do help readin bash for details.

获取行的关键是read命令。不要help read在bash了解详情。

Update

更新

Okay, so let's look a little more closely. Start the script with

好的,让我们仔细看看。启动脚本

Let's call the script a.bash. You'll want to run it as

我们将脚本称为 a.bash。你会想要运行它

 $ a.bash b.txt < a.txt > a_match.txt  

a.bashis a script that reads line from a.txt, searches in b.txtand sends output to STDOUT which you then direct into your file a_match.txt .

a.bash是一个脚本,它从 a.txt 读取行,搜索b.txt并将输出发送到 STDOUT,然后您将其定向到您的文件 a_match.txt 中。

Start the script witha "shebang line'. Usually it's

以“shebang line”开始脚本。通常是

 #!/usr/bin/bash --

or something similar.

或类似的东西。

The readprimitive simply reads a line into a variable, and by default reads into a variable named REPLY. So "there are lines left in the file, read a line" is simply

read原始简单的读取一行到一个变量,并且默认读取到一个变量命名的答复。所以“文件中还有行,读取一行”很简单

 while read

The name of the file to search in will be in the special shell variable $1, representing the first argument. Since the other names are used in redirection, they don't appear as arguments at all.

要搜索的文件名将在特殊的 shell 变量 $1 中,代表第一个参数。由于其他名称用于重定向,因此它们根本不作为参数出现。

Now, the command grep(1)searches a file for a string and put's the line on which the string occurs onto STDOUT. So all we need for the search is

现在,命令grep(1)在文件中搜索字符串并将出现字符串的行放到 STDOUT 上。所以我们搜索所需的只是

 grep $REPLY b.txt

(Read the man page for grep.)

(阅读grep的手册页。)

Since grep(1)puts the output on STDOUT anyway, it'll go out and be redirected into a_matches.txt. That's your "grep the line in the file" line.

由于grep(1)无论如何都会将输出放在 STDOUT 上,因此它将出去并被重定向到a_matches.txt. 那是您的“grep 文件中的行”行。

Now just fit that all together.

现在只需将它们组合在一起即可。

Quiz:

测验:

  1. Why is it $REPLY?
  2. How would you change the script so you could call it as:

    $ a.bash a.txt b.txt a_matches.txt

  1. 为什么$REPLY
  2. 您将如何更改脚本,以便您可以将其称为:

    $ a.bash a.txt b.txt a_matches.txt

回答by freiheit

Here's a quick solution that I think meets your requirements:

这是我认为满足您要求的快速解决方案:

fgrep -x -f b.txt a.txt > a_match.txt

回答by Algorithmist

This could help you just go through the tutorial and it bests meet your requirements.Again there is not just one way to read a file line by line and some are faster than others and some are more intuitive than others.

这可以帮助您完成本教程,它最能满足您的要求。同样,逐行读取文件的方法不止一种,有些方法比其他方法快,有些方法比其他方法更直观。

http://www.unix.com/tips-tutorials/18009-12-ways-parse-file.html

http://www.unix.com/tips-tutorials/18009-12-ways-parse-file.html

回答by Paused until further notice.

This is what I would use:

这就是我会使用的:

diff dir1/ dir2/

or

或者

diff "" ""