We can use any of the below commands if we are required to write the output to a new file. But, we can only use
sedandperlcommands if instructed to remove blank lines and lines containing whitespace characters and store the updated content in the original file.
Using grep Command
Use the grep command to remove blank lines and lines with whitespace characters from the specified text file in bash.
|
1 2 3 4 5 |
grep -v '^[[:space:]]*$' file.txt > outFile.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" echo -e "The contents of 'outFile.txt' are:\n$(cat outFile.txt)" |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. The contents of 'outFile.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
Alternatively, we can use the grep command as follows to remove blank lines and lines with whitespace characters from the specified text file in bash.
|
1 2 3 4 5 |
grep -v '^[[:blank:]]*$' file.txt > outFile.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" echo -e "The contents of 'outFile.txt' are:\n$(cat outFile.txt)" |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. The contents of 'outFile.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
The grep command searched for the blank lines and lines with whitespace characters in the file.txt file, removed them, and wrote the updated content in the outFile.txt file. Then, the echo commands were used with the cat command to display the content of the specified file. Let’s break down the commands to understand their components below:
- The
grepcommand isGlobal Regular Expression Print. A command-line utility searched the given patterns in the specified text files. - The
-voption informed thegrepcommand to invert the match, which means printing all lines that don’t match the given pattern. - The
'^[[:space:]]*$'regex pattern matched blank lines and lines with whitespace characters. The^matched the start of the line,$matched the line’s end, and[[:space:]]matched any whitespace characters (tab or space). So, the'^[[:space:]]*$'pattern matched the lines having zero or more whitespace characters between the start and end of a line. Remember to enclose the regex within the quotes. - The
'^[[:blank:]]*$'did the same as'^[[:space:]]*$'. It is just an alternate way to write regex. - The
file.txtis the input file thegrepcommand uses to search for empty lines. - The
>is the redirection operator, which redirected the output of thegrepcommand to theoutFile.txtfile. If it already exists, the>operator will overwrite theoutFile.txtfile. You can also use>>if you don’t want to overwrite theoutFile.txtbut append it. - The
$(cat file.txt)and$(cat outFile.txt)are command substitutions used to run thecatcommand on the specified file, capturing the output as a string. This output was printed on the terminal (bash console). - The
echocommands were used to print the customized output on the bash console. - In the
echocommands, we used the-eparameter to interpret the\ncharacter. - The
catcommand was used to retrieve (read) the content of the specified file.
Using awk Command
Use the awk command to remove blank lines and lines with whitespace characters from the specified text file in bash.
|
1 2 3 4 5 |
awk 'NF' file.txt > outFile.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" echo -e "The contents of 'outFile.txt' are:\n$(cat outFile.txt)" |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. The contents of 'outFile.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
We have already learned about the echo command and its components. So here we are breaking down the awk command to understand it and its components.
- The
awkis a text processing utility capable of filtering, searching and manipulating the specified text files. - The
'NF'is an awk pattern matched any line with at least one field, which was any non-empty line. Why did it work in this way? Because, inawk, a line is split into fields based on the delimiter, it is whitespace by default. Note that theNFis a built-in variable representing the fields’ number on a given line. file.txtwas the input file name, whileoutFile.txtwas the output file name.
Using sed Command
Use the sed command to remove blank lines and lines with whitespace characters from the specified text file in bash.
|
1 2 3 4 5 |
sed '/^[[:space:]]*$/d' file.txt > outFile.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" echo -e "The contents of 'outFile.txt' are:\n$(cat outFile.txt)" |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. The contents of 'outFile.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
Use the sed command to remove blank lines and lines with whitespace characters from the specified text file in bash and write the output to the original file.
|
1 2 3 4 |
sed -i '/^[[:space:]]*$/d' file.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" |
|
1 2 3 4 5 6 7 8 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
We have already learned about echo and its components in the first section while learning the grep command. Let’s break down the sed command to understand it.
- The
sedis a stream editor command; we used it to manipulate the specified text files. - The
/^[[:space:]]*$/pattern matched all blank lines whether they have whitespace characters. - The
/dis asedcommand, instructing it to delete the matched lines. - The
file.txtwas the input file name whileoutFile.txtwas the output file name. - The
-ioption was used to update the original file, which wasfile.txtin the above examples. While using the-ioption, the input and output file names were the same,file.txt.
Further reading:
Using perl Command
Use the perl command to remove blank lines and lines with whitespace characters from the specified text file in bash.
|
1 2 3 4 5 |
perl -ne 'print if /\S/' file.txt > outFile.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" echo -e "The contents of 'outFile.txt' are:\n$(cat outFile.txt)" |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. The contents of 'outFile.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
Use the perl command to remove blank lines and lines with whitespace characters from the specified text file in bash and write the output to the original file.
|
1 2 3 4 |
perl -i -ne 'print if /\S/' file.txt echo -e "The contents of 'file.txt' are:\n$(cat file.txt)" |
|
1 2 3 4 5 6 7 8 |
The contents of 'file.txt' are: It is line1. It is line2. It is line3. It is line4. It is line5. |
In the first section, we learned about echo while using the grep command. Here, we will focus on the perl command and its components; let’s break it down to understand.
- The
perlis the Perl interpreter’s name; we used it to execute the Perl code. - The
-neoption was used to instruct the Perl to read the file content (the content offile.txt) line by line and execute the'print if /\S/'code for every line. - The
'print if /\S/'was the Perl code we ran it for every line of thefile.txtfile. This code snippet used theprintstatement to output every line offile.txtbut only if it matched the given regex,\S.The\Smatched one or multiple blank lines and lines containing whitespace characters. - The
file.txtwas the input file name whileoutFile.txtwas the output file name. - The
-ioption told Perl to update the original file in place, which wasfile.txtin the above examples. While using the-ioption, the input and output file names were the same,file.txt.
If your input file contains blank lines only (does not have lines containing whitespace characters), then you can use
grep,tr,awk,sedandperlcommands as follows; do not forget to replace thefile.txtwith your input file name andoutFile.txtwith your output file name.
grep -v '^$' file.txt > outFile.txttr -s '\n' outFile.txtawk 'NF' file.txt > outFile.txtsed '/^$/d' file.txt > outFile.txtorsed -i '/^$/d' file.txtperl -ne 'print unless /^$/' file.txt > outFile.txtorperl -i -ne 'print unless /^$/' file.txt
That’s all about bash remove blank lines from File.