Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

remove blank lines with grep command

 In this tutorial, How do remove blank lines with grep command. grep command is used to search text and strings in a given file.

For example 1

The content grep_command.txt file as below

welcome to huuphan.com
I'm Huu
[[email protected]$]
other web site devopsroles.com
[[email protected]$]

[[email protected]$] test
Tip tip

The result,
welcome to huuphan.com
I'm Huu
other web site devopsroles.com

Tip tip

Use command below:
grep -v "\$[[:space:]]*" grep_command.txt

The output terminal as below:

remove blank lines with grep command

For example 2

The content grep_command2.txt file as below

phan

van



huu

The result,
phan
van
huu

Use command as below:
grep -v "^[[:space:]]*$" grep_command2.txt
# or
grep -v "^$" grep_command2.txt

The output terminal as below:

remove blank lines with grep command 2

Explained

The -v makes it print lines that do not completely match

^             match start of line
[[:space:]]   match whitespace- spaces, tabs, carriage returns, etc.
*             previous match (whitespace) may exist from 0 to infinite times
$             match end of line

post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com