在unix shell脚本中查找一个文件的内容到另一个文件中

时间:2022-11-15 21:49:06

I'm using following shell script to find 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 am I doing?

我正在执行脚本,但它没有显示csv文件中的内容。我的contents.txt文件包含诸如“08915673”或“123223”之类的数字,它们也存在于csv文件中。我做错了吗?

3 个解决方案

#1


29  

grep itself 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.

是每行包含一个模式的文件;和 是您要在其中搜索内容的文件。

Note that, to force grep to 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; grep itself offers this feature.

你根本不需要在这里循环; 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.

您可以将“,”更改为文件中的分隔符。

#2


2  

Another solution:

另一种方案:

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

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

   if(FILENAME == nowfile)
   {
     hash[$0]=$0;
   }
   else
   {
       if($0 ~ hash[$0])
       {  
           print $0
       }
   }
} '  xx yy

#3


1  

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

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

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

一个命令就足够了。在我的情况下,需要一个csv文件中第11列的标识号(“;”作为分隔符)

grep -f <(awk -F";" '{print $11}' FILE_TO_EXTRACT_PATTERNS_FROM.csv) TARGET_FILE.csv 

I hope this helps.

我希望这有帮助。

#1


29  

grep itself 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.

是每行包含一个模式的文件;和 是您要在其中搜索内容的文件。

Note that, to force grep to 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; grep itself offers this feature.

你根本不需要在这里循环; 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.

您可以将“,”更改为文件中的分隔符。

#2


2  

Another solution:

另一种方案:

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

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

   if(FILENAME == nowfile)
   {
     hash[$0]=$0;
   }
   else
   {
       if($0 ~ hash[$0])
       {  
           print $0
       }
   }
} '  xx yy

#3


1  

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

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

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

一个命令就足够了。在我的情况下,需要一个csv文件中第11列的标识号(“;”作为分隔符)

grep -f <(awk -F";" '{print $11}' FILE_TO_EXTRACT_PATTERNS_FROM.csv) TARGET_FILE.csv 

I hope this helps.

我希望这有帮助。