How to extract or remove lines with grep?

 


In this post, we go through some examples of extracting or removing lines of a file with the grep command line tool.

Suppose we have a file text.txt with the following content.


sed command stands for stream editor
sed allows us to substitute, insert or delete, find and replace strings in a file
sed is quite useful and sed is awesome!

How to filter lines having "useful"? 

Command:

$grep useful text.txt
Output:

sed is quite useful and sed is awesome!

How to filter those lines with case insensitive? 

For example, filtering lines with sed or SED

Suppose we have an updated file text.txt with the following content.


SED command stands for stream editor
SED allows us to substitute, insert or delete, find and replace strings in a file
SED is quite useful and sed is awesome!
Command (with -i option):

$grep -i sed
Ouput:

SED command stands for stream editor
SED allows us to substitute, insert or delete, find and replace strings in a file
SED is quite useful and sed is awesome!

How to filter lines with regex patterns? 

For example, lines starting with SED and containing substitute 


Command (using -E option):

$grep -E '^SED.*substitute.*'
Ouput:

SED allows us to substitute, insert or delete, find and replace strings in a file

No comments:

Post a Comment