从文件的每一行中获取数值,然后在Unix中搜索另一个文件中的这些数值。

huangapple go评论69阅读模式
英文:

Take value from each line of a file and search for it in another file in Unix?

问题

I have two files. I want my Unix code to take each first column from each line of File A as a variable and look for it in File B. If found, I want the found line in File B to be saved in an output file.

File A:
1335 skrgrsjgls 1231
12213 23rerwsdf 9605
11567 4693460 0340592
54321 293402460 324

File B:
-54321 Apples
-23453 Grapes
-1335 Pears
-65363 Pineapples
-96506 Grapefruit

Expected Output File:
-1335 Pears
-54321 Apples

英文:

I have two files. I want my Unix code to take each first column from each line of File A as a variable and look for it in File B. If found, I want the found line in File B to be saved in an output file.

File A:
1335 skrgrsjgls 1231
12213 23rerwsdf 9605
11567 4693460 0340592
54321 293402460 324

File B: 
-54321 Apples
-23453 Grapes
-1335 Pears
-65363 Pineapples
-96506 Grapefruit

Expected Output File:
-1335 Pears
-54321 Apples

Code I have tried:

    for i in $(cat File A); do grep -w $i File B > FileC; done

I get no result for this. Please any help is appreciated.

答案1

得分: 1

I hope it works for you. This could be done with grep command which is used to search patterns in the file.

OUTPUT

这个希望对你有帮助。这可以使用 `grep` 命令来完成,该命令用于在文件中搜索模式。

this one is the output for yours tested command.

这是您测试的命令的输出。
英文:

I hope it works for you. This could be done with grep command which is used to search patterns in the file.

for i in $(cat FileA.txt); do   grep -i "$i" FileB.txt >> output.txt; done

OUTPUT

HaLF-MenTaL@DESKTOP-7B47GH0 MINGW64 ~/Documents
$ for i in $(cat FileA.txt); do   grep -i "$i" FileB.txt >> output.txt; done

HaLF-MenTaL@DESKTOP-7B47GH0 MINGW64 ~/Documents
$ cat output.txt
-1335 Pears
-54321 Apples

this one is the output for yours tested command.

HaLF-MenTaL@DESKTOP-7B47GH0 MINGW64 ~/Documents
$ for i in $(cat FileA.txt); do grep -i "$i" FileB.txt | head -10; done
-1335 Pears
-54321 Apples

答案2

得分: 1

对于大文件,你应该避免在每一行中使用新的grep循环。

awk是一个很好的工具,另一个解决方案是:

grep -f <(sed -r 's/([^ ]*).*/^- /' FileA) FileB

部分 <(sed -r 's/([^ ]*).*/^-\1 /' FileA) 创建了一个类似于内存中的字符串文件,其中包含你想要查找的字符串(以 ^ 开头,连字符,数字,空格),
grep -f file 使用 "file" 作为要查找的多个字符串。

英文:

For large files you should avoid loops with a new grep for each line.
awk is a great tool, an other solution is

grep -f &lt;(sed -r &#39;s/([^ ]*).*/^- /&#39; FileA) FileB

The part &lt;(sed -r &#39;s/([^ ]*).*/^-\1 /&#39; FileA) is creating a kind of in-memory file of strings you want to look for (start of line with ^, hyphen, number, space),
and grep -f file uses the "file" as multiple strings to look for.

答案3

得分: 1

好的,以下是已解决我的问题的内容。我感谢所有为这个问题做出贡献的人,我相信他们的版本也有效。

while read -r line; do var=$(echo $line | awk '{print $1}') grep -w "$var" FileB > FileC; done < FileA
英文:

Okay, so the following solved my problem. I thank everyone who contributed to the question and I am sure their version also works.

    while read -r line; do var=$(echo $line | awk &#39;{print $1}&#39;) grep -w &quot;$var&quot; FileB &gt; FileC; done &lt; FileA

答案4

得分: 1

我同意应该避免在处理大文件时使用循环,并且awk是一个很好的替代方案。尝试使用以下命令:

awk 'FNR==NR { a[$1]=$0; next } $1 in a { print a[$1] }' FileB.txt FileA.txt > Output.txt

其中:

  • FNR==NR 部分确保下一个块仅对第一个文件执行,
  • { a[$1]=$0; next } 会将 FileB.txt 的第一列保存为数组,
  • $1 in a 会检查该键是否存在于 FileA.txt 中,
  • { print a[$1] } 打印找到的匹配项,
  • FileB.txt FileA.txt 是命令行中提供的参数(注意顺序),
  • > Output.txt 将打印内容保存到输出文件中。
英文:

I agree that loops should be avoided for large files and that awk is a good alternative. Try doing:

awk &#39;FNR==NR { a[$1]=$0; next } $1 in a { print a[$1] }&#39; FileB.txt FileA.txt &gt; Output.txt

The FNR==NR part ensure that the next block is only executed for the first file,
{ a[$1]=$0; next } will save the first col of FileB.txt as an array,
$1 in a will check if that key exists in FileA.txt,
{ print a[$1] } prints the matches that are found,
FileB.txt FileA.txt are the arguments given to the cmd line (careful with the order),
and &gt; Output.txt saves that print to an output file.

huangapple
  • 本文由 发表于 2023年6月9日 05:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定