bash grep 文件中的每一行

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

grep each line in a file

linuxbashgrep

提问by smilingbuddha

I have two files: one is a fairly long collection of names (names.txt), and another file (grades.csv) which is a huge file of names and the corresponding grades. I would like to iterate over each line in names.txt and extract that name in grades.csv with the entire matching line.

我有两个文件:一个是相当长的名称集合 (names.txt),另一个文件 (grades.csv) 是一个巨大的名称和相应成绩文件。我想遍历 names.txt 中的每一行,并使用整个匹配行在 grades.csv 中提取该名称。

This is how a small sample names.txt looks like

这是一个小样本 names.txt 的样子

"Dumbledore, Albus"
"Potter, Harry"
"Riddle, Tom

Here is the structure of a dummy grades.csv file

这是一个虚拟的grades.csv文件的结构

"Granger, Hermione", 96.65%, 9,10
"Mcgonagall, Minerva", 80.43%, 6,7
"Dumbledore, Albus", 100%, 8, 9
"Potter, James", 91%, 7,89
"Ravenclaw, Rowena", 32%, 4,56
"Potter, Harry", 34%, 56,67
"Riddle, Tom", 99%, 3,4

I'd like to extract each line of names.txt and search grades.csv to get this

我想提取names.txt的每一行并搜索grades.csv来得到这个

"Dumbledore, Albus", 100%, 8, 9
"Potter Harry", 34%, 56,67
"Riddle Tom", 99%, 3,4

I know I would have to use grep/awk/sed for this (I am using a Linux environment) but I don't know how to use grep to loop over the lines in a file, since I am not very good at the bash command terminal. Any help appreciated!

我知道我必须为此使用 grep/awk/sed(我使用的是 Linux 环境)但我不知道如何使用 grep 来循环文件中的行,因为我不太擅长 bash命令终端。任何帮助表示赞赏!

回答by erip

I made some changes to your names.txtand grades.csv- some of the names are comma-separated and some aren't. I removed commas within quotes, so here are the new files:

我对您的names.txt和进行了一些更改grades.csv- 有些名称以逗号分隔,有些则不是。我删除了引号内的逗号,所以这里是新文件:

22:46 $ cat names.txt 
"Dumbledore Albus"
"Potter Harry"
"Riddle Tom"

22:46 $ cat grades.csv 
"Granger Hermione", 96.65%, 9,10
"Mcgonagall Minerva", 80.43%, 6,7
"Dumbledore Albus", 100%, 8, 9
"Potter James", 91%, 7,89
"Ravenclaw Rowena", 32%, 4,56
"Potter Harry", 34%, 56,67
"Riddle Tom", 99%, 3,4

You can use grepwith a file argument -f:

您可以使用grep文件参数-f

22:46 $ cat script.sh 
#!/bin/bash
names="/path/to/names.txt"
grades="/path/to/grades.csv"
grep -f <(tr ',' '\n' < "${names}") "${grades}"

This gives me the following output:

这给了我以下输出:

22:46 $ ./script.sh 
"Dumbledore Albus", 100%, 8, 9
"Potter Harry", 34%, 56,67
"Riddle Tom", 99%, 3,4

EDIT

编辑

Assuming the names.txtand grades.csvas a rule are formatted as "Lastname, Firstname" case-insensitively:

假设names.txtgrades.csv作为规则格式为“姓氏,名字”,不区分大小写:

#!/bin/bash
names="/path/to/names.txt"
grades="/path/to/grades.csv"
grep -fi "${names}" "${grades}"

回答by Nguyen Sy Thanh Son

Try this:

尝试这个:

  while read l; do grep -i "${l//\"/}" grades.csv; done < names.txt 

I tested it with bashon Ubuntu 14.04. The output:

bash在 Ubuntu 14.04 上对其进行了测试。输出:

$ while read l; do grep -i "${l//\"/}" grades.csv; done < names.txt 
"Dumbledore, Albus", 100%, 8, 9
"Potter, Harry", 34%, 56,67
"Riddle, Tom", 99%, 3,4