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
[server@xxx$]
other web site devopsroles.com
[server@xxx$]

[server@xxx$] 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

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

How to install and configure zimbra multi server

Bash script list all IP addresses connected to Server