Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal command to find lines containing a specific word?

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

like image 911
willykao Avatar asked Jan 31 '12 04:01

willykao


1 Answers

grep is your friend.

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...)

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

like image 158
Eric B. Avatar answered Sep 19 '22 11:09

Eric B.