如何在UNIX中打印出包含特定字符串的文件的所有行 [英] How can I print out all lines of a file containing a specific String in unix

查看:38
本文介绍了如何在UNIX中打印出包含特定字符串的文件的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何打印出String的第一个匹配项,但是我不确定如何打印出所有的匹配项.要打印出我第一次使用过的String,

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. To print out the first occurrence of my String I have used

grep ",1," my_course.

我感觉要打印出所有出现的样子:

I feel like printing out all occurrences would look something like:

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

我不确定如何在while循环中执行此操作.如何测试文件是否有下一行.

I'm not sure how to do this while loop though. How do I test if file has next line.

对不起,我以为grep只匹配第一次出现.当我运行它时,它只匹配1行.我不知道只有1行匹配

Sorry guys I thought grep only matched the first occurrence. When I ran it, it only matched 1 line. I didn't know only 1 line matched

此外,如果有人想回答...奖励问题...如果我文件(my_course)中的所有行都看起来像数字,名称,学时,成绩",我将如何获取文件中的所有成绩并将其存储到数组?

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?

推荐答案

grep 已打印出所有匹配项.

grep already prints out all occurrences.

来自grep的手册页:

grep,egrep,fgrep-打印与模式匹配的

这篇关于如何在UNIX中打印出包含特定字符串的文件的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆