简体   繁体   English

如何在unix中打印出包含特定String的文件的所有行

[英]How can I print out all lines of a file containing a specific String in unix

I know how to print out the line of the first occurrence of the String but I'm not sure how to print out all occurrences. 我知道如何打印出第一次出现的String的行,但我不知道如何打印出所有出现的内容。 To print out the first occurrence of my String I have used 打印出我用过的第一次出现的String

grep ",1," my_course.

I feel like printing out all occurrences would look something like: 我觉得打印出来的所有事件看起来都像是:

    while(file has next line)
         grep ",1," my_course

I'm not sure how to do this while loop though. 我不知道如何在循环时这样做。 How do I test if file has next line. 如何测试文件是否有下一行。

EDIT: Sorry guys I thought grep only matched the first occurrence. 编辑:对不起家伙我认为grep只匹配第一次出现。 When I ran it, it only matched 1 line. 当我运行它时,它只匹配1行。 I didn't know only 1 line matched 我不知道只有1行匹配

Also, bonus question if someone wants to answer... if all the lines in my file (my_course) look like "number,name,credit hours,grade" how would I get all the grades in the file and store into an array? 此外,如果有人想要回答的话还有额外的问题...如果我的文件中的所有行(my_course)看起来像“数字,名称,学分,成绩”,我将如何获得文件中的所有成绩并存储到数组中?

grep already prints out all occurrences. grep已打印出所有出现的内容。

From the man pages of grep: 从grep的手册页

grep, egrep, fgrep - print lines matching a pattern grep,egrep,fgrep - 打印与图案匹配的线条

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UNIX:删除所有包含字符串和数字的行 - UNIX : Deleting all the lines containing string & number 如何将所有不包含特定文本的行重定向到文本文件? - How to redirect all lines not containing specific text to a text file? 如何删除包含特定模式并具有“模式文件”的行? - How can I remove lines containing a specific pattern and have a “pattern file”? 如何grep文件然后将其打印出来? Unix的 - How to grep a file then print it out? Unix 使用bash,如何查找所有包含特定字符串的文件并将其替换为现有文件? - Using bash, how do I find all files containing a specific string and replace them with an existing file? 我如何从bash变量中回显/打印特定行 - how can I echo/print specific lines from a bash variable 如何在仍写入同一文件的同时过滤掉特定行? - How can I filter out specific lines while still writing to the same file? 替换bash中包含特定字符串的所有行中的第二个条目 - Replace the second entry in all lines containing a specific string in bash 如何从多个文件中删除特定字符串之前的所有行 - How can I delete all lines before a specific string from a number of files 打印包含字符串的行和第一个空行之间的所有行,从包含该字符串的行开始 - Print all lines between line containing a string and first blank line, starting with the line containing that string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM