You can use any one of the following utility to delete all empty lines from text file:
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | N/A |
Syntax
The syntax is:
command input.txt > output.txt command [option] input.txt > output.txt sed '...' input.txt > output.txt ## Gnu/sed sed -i '...' input.txt awk '...' input.txt > output.txt
sed command examples
Type the following sed command to delete all empty files:
sed '/^$/d' input.txt > output.txt cat output.txt
OR
sed -i '/^$/d' input.txt cat input.txt
awk command examples
Type the following awk command to delete all empty files:
awk 'NF > 0' input.txt > output.txt cat output.txt
perl command examples
Type the following perl one liner to delete all empty files and save orignal file as input.txt.backup:
perl -i.backup -n -e "print if /\S/" input.txt
Verify updated files, type:
cat input.txt cat input.txt.backup
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 4 comments so far... add one ↓
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
You can also use grep :)
cat output.txt | grep -v “^$” | less
suppress repeated empty output lines
I am using grep also, but like this:
grep . input.txt > output.txt
There is a dot between grep and input.txt.
you can also do this by:
grep -v ‘^ ‘ $filename > $newfile
please note that there is space after ^ in above example.
-Kuldeep Kulkarni