终端命令查找包含特定单词的行?

时间:2021-02-23 19:13:01

I was just wondering what command i need to put into the terminal to read a text file, eliminate all lines that do not contain a certain keyword, and then print those lines onto a new file. for example, the keyword is "system". I want to be able to print all lines that contain system onto a new separate file. Thanks

我只是想知道,我需要把什么命令放到终端来读取文本文件,删除所有不包含某个关键字的行,然后将这些行打印到一个新文件上。例如,关键字是“system”。我希望能够将包含系统的所有行打印到一个新的独立文件中。谢谢

4 个解决方案

#1


18  

grep is your friend.

grep是你的朋友。

For example, you can do:

例如,你可以这样做:

grep system <filename> > systemlines.out

man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)

你也可以得到更多有用的信息(例如:行号,前一行+行,后一行+行-例如:所有不包含grep的行,等等)

If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.

如果您正在运行Windows,您可以安装cygwin,也可以为grep找到win32二进制文件。

#2


3  

grep '\<system\>'

Will search for lines that contain the word system, and not system as a substring.

将搜索包含单词system而不是system作为子字符串的行。

#3


2  

below grep command will solve ur problem

下面的grep命令将解决您的问题

grep -i yourword filename1 > filename2

with -i for case insensitiveness

和-我的情况不敏感。

without -i for case sensitiveness

没有-i的病例敏感性

to learn how grep works on ur server ,refer to man page on ur server by the following command

要了解grep在ur服务器上的工作方式,请通过以下命令参阅ur服务器上的man页面

man grep

#4


-1  

grep "system" filename > new-filename

grep“system”文件名>新文件名

You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

你可能想让它更聪明一点,不包括像“dy系统性”这样的词,但这是一个很好的开始。

#1


18  

grep is your friend.

grep是你的朋友。

For example, you can do:

例如,你可以这样做:

grep system <filename> > systemlines.out

man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)

你也可以得到更多有用的信息(例如:行号,前一行+行,后一行+行-例如:所有不包含grep的行,等等)

If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.

如果您正在运行Windows,您可以安装cygwin,也可以为grep找到win32二进制文件。

#2


3  

grep '\<system\>'

Will search for lines that contain the word system, and not system as a substring.

将搜索包含单词system而不是system作为子字符串的行。

#3


2  

below grep command will solve ur problem

下面的grep命令将解决您的问题

grep -i yourword filename1 > filename2

with -i for case insensitiveness

和-我的情况不敏感。

without -i for case sensitiveness

没有-i的病例敏感性

to learn how grep works on ur server ,refer to man page on ur server by the following command

要了解grep在ur服务器上的工作方式,请通过以下命令参阅ur服务器上的man页面

man grep

#4


-1  

grep "system" filename > new-filename

grep“system”文件名>新文件名

You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

你可能想让它更聪明一点,不包括像“dy系统性”这样的词,但这是一个很好的开始。