How To Grep Words From a File in Unix / Linux

How To Grep Words From a File in Unix / Linux

To search for a specific word or pattern in a file using the grep command in Unix or Linux, you can use the following syntax:

grep "pattern" file
Sourc‮‬e:www.lautturi.com

For example, to search for the word "test" in the file file.txt, you can use the following command:

grep "test" file.txt

This will print all the lines in the file file.txt that contain the word "test."

You can also search for patterns that use regular expressions. For example, to search for a word that starts with "test" and ends with "ing," you can use the following command:

grep "test.*ing" file.txt

This will print all the lines in the file file.txt that contain a word that starts with "test" and ends with "ing."

You can also use the -v option to print only the lines that do not contain the pattern. For example:

grep -v "test" file.txt

This will print all the lines in the file file.txt that do not contain the word "test."

Created Time:2017-10-28 21:38:52  Author:lautturi