Removing Blank Lines Using the Grep Command

In this tutorial, we'll explore how to utilize the grep command to eliminate blank lines. The grep command proves invaluable for searching text and strings within files, providing efficient ways to manage content. Learn the process of removing unwanted blank lines with ease and precision.

For example 1

The content grep_command.txt file is as follows:

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
In conclusion, mastering the grep command enables efficient text searching and manipulation, making it a valuable tool in a developer's arsenal. Whether it's removing blank lines or performing complex pattern matching, grep streamlines tasks and boosts productivity. Embrace its capabilities to enhance your command-line proficiency. I hope will this your helpful. Thank you for reading the huuphan.com page!

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server

How to install php7 on centos 6