foo bar foobar
How can I delete all leading and/or trailing blank spaces, tab from each line using sed command?
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | sed/awk/perl |
Time | 2 minutes |
- Perl.
- Python.
- Awk and friends.
Perl example
The syntax is:
perl -lape 's/\s+//sg' input > output
Sample outputs:
foo bar foobar
Or use -pie syntax to update file:
cat input perl -lapi -e 's/\s+|^\n//sg' input cat input
See perl man page for more information.
Sed example
The syntax is:
sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' input > output
OR updated file in a single go with the -i option:
sed -i -e 's/^[ \t]*//' -e 's/[ \t]*$//' input
See sed command man page for more information.
Awk example
The syntax is
awk '{$1=$1}{ print }' input > output
You can also use gsub() substring matching the regular expression function. See awk man page for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 5 comments so far... add one ↓
🐧 5 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 |
It seems generally you should choose perl over sed or awk if you don’t care about speed too much. Because you probably would be tempted to use some advanced features only provided by GNU sed and awk, which not likely appearing in OS X or other BSD variants by default.
Hi,
Thanks for nice article.
The tr command is also usefull.
For instance, I use it this way in a video conversion script to strip – actually replace them with underscore, but you can really strip them with tr – all annoying characters from a filename (often generated by windows) :
goodfilename=`echo -n ${badfilename}|tr ‘[:space:] ( ) &’ _`
If badfilename is for instance :
Me & My Children during Christmas (short version).mp4
It will be converted to
Me___My_Children_during_Chritsmas_short_version_.mp4
which avoid further errors in script.
Hi,
Thanks a lot for the command. Sed command worked perfectly.
can you please explain me the way the command worked ?. Sorry if i am annoying most of you ppl here with this silly question but i am serioulsy curious to know how it worked.i am very new to linux and scripting , couldn’t figure out how it worked.
thanks a lot for the command again.would be very greatful if my query is answered.
cheers
I have the below file,
TEST.DAT –> contain all spaces
I need to remove all the spaces
Please let me know what is the command i can use
and am using tail -1 TEST.DAT as a variable $TEST
and then check $TEST is empty by removing all the spaces
so the unix command should exit if $TEST is blank