Linux 在另一个文件中查找一个文件的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15059422/
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-08-06 19:06:51  来源:igfitidea点击:

Finding contents of one file in another file

linuxshellunixawkgrep

提问by NIMISH DESHPANDE

I'm using the following shell script to find the contents of one file into another:

我正在使用以下 shell 脚本将一个文件的内容查找到另一个文件中:

#!/bin/ksh
file="/home/nimish/contents.txt"

while read -r line; do
    grep $line /home/nimish/another_file.csv
done < "$file"

I'm executing the script, but it is not displaying the contents from the CSV file. My contents.txt file contains number such as "08915673"or "123223"which are present in the CSV file as well. Is there anything wrong with what I do?

我正在执行脚本,但它没有显示 CSV 文件中的内容。我的 contents.txt 文件包含CSV 文件中也存在的诸如"08915673"或 之类的数字"123223"。我的做法有什么问题吗?

采纳答案by Rubens

grepitself is able to do so. Simply use the flag -f:

grep本身就能够做到这一点。只需使用标志-f

grep -f <patterns> <file>

<patterns>is a file containing one pattern in each line; and <file>is the file in which you want to search things.

<patterns>是一个文件,每行包含一个模式;并且<file>是您要在其中搜索内容的文件。

Note that, to force grepto consider each line a pattern, even if the contents of each line look like a regular expression, you should use the flag -F, --fixed-strings.

请注意,要强制grep将每一行视为一个模式,即使每一行的内容看起来像一个正则表达式,您也应该使用标志-F, --fixed-strings

grep -F -f <patterns> <file>

If your file is a CSV, as you said, you may do:

如果您的文件是 CSV,如您所说,您可以执行以下操作:

grep -f <(tr ',' '\n' < data.csv) <file>


As an example, consider the file "a.txt", with the following lines:

例如,考虑文件“a.txt”,其中包含以下几行:

alpha
0891234
beta

Now, the file "b.txt", with the lines:

现在,文件“b.txt”,有以下几行:

Alpha
0808080
0891234
bEtA

The output of the following command is:

以下命令的输出是:

grep -f "a.txt" "b.txt"
0891234

You don't need at all to for-loop here; grepitself offers this feature.

你根本不需要在for这里循环;grep本身提供了这个功能。



Now using your file names:

现在使用您的文件名:

#!/bin/bash
patterns="/home/nimish/contents.txt"
search="/home/nimish/another_file.csv"
grep -f <(tr ',' '\n' < "${patterns}") "${search}"

You may change ','to the separator you have in your file.

您可以更改','为文件中的分隔符。

回答by sharingli

Another solution:

另一种解决方案:

  • use awkand create your own hash(e.g. ahash), all controlled by yourself.
  • replace $0 to $iand you can match any fields you want.
  • 使用awk和创建您自己的hash(例如ahash),全部由您自己控制。
  • 替换$0 to $i,您可以匹配您想要的任何字段。


awk -F"," '
{  
   if (nowfile==""){ nowfile = FILENAME;  }

   if(FILENAME == nowfile)
   {
     hash[
grep -f <(awk -F";" '{print }' FILE_TO_EXTRACT_PATTERNS_FROM.csv) TARGET_FILE.csv
]=##代码##; } else { if(##代码## ~ hash[##代码##]) { print ##代码## } } } ' xx yy

回答by Philippe Delteil

I don't think you really need a script to perform what you're trying to do.

我不认为你真的需要一个脚本来执行你想要做的事情。

One command is enough. In my case, I needed an identification number in column 11 in a CSV file (with ";" as separator):

一个命令就够了。就我而言,我需要一个 CSV 文件第 11 列中的标识号(使用“;”作为分隔符):

##代码##