Here our requirement is to remove all the whitespace from the leading and ending space per line of a file.
Our sample file /tmp/file
As you see the "tab" space is still there
To remove all leading whitespaces (including tabs)
Here I have combined both the commands with ";"
Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
How to configure Samba 4 Secondary Domain Controller
How to secure Apache web server in Linux using password (.htaccess)
How to register Red Hat Linux with RHN (Red Hat Network )
Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
15 tips to enhance security of your Linux machine
Why is Linux more secure than windows and any other OS
What is the difference between "su" and "su -" in Linux?
What is kernel-PAE in Linux?
What is swappiness and how do we change its value?
How to log iptables messages in different log file
What are the s and k scripts in the etc rcx.d directories
What is virtual memory, paging and swap space?
10 examples to help you understand top command usage in Unix/Linux
Our sample file /tmp/file
This is line one
This is line two
This is line three
This is line four
Remove all leading whitespace
To remove all tab whitespace (This will not remove empty space)# sed 's/^[ ]*//g' /tmp/file
This is line one
This is line two
This is line three
This is line four
As you see the "tab" space is still there
To remove all leading whitespaces (including tabs)
# sed 's/^[\t ]*//g' /tmp/file
This is line one
This is line two
This is line three
This is line four
Remove all trailing whitespace
To remove all trailing whitespaces (including tabs)# sed -i 's/[\t ]*$//g' /tmp/file
Remove both leading and trailing whitespaces
# sed 's/^[\t ]*//g;s/[\t ]*$//g' /tmp/file
This is line one
This is line two
This is line three
This is line four
Here I have combined both the commands with ";"
Follow the below links for more tutorials
Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, OpenfilerTutorial for Monitoring Tools SAR and KSAR with examples in Linux
How to configure Samba 4 Secondary Domain Controller
How to secure Apache web server in Linux using password (.htaccess)
How to register Red Hat Linux with RHN (Red Hat Network )
Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
15 tips to enhance security of your Linux machine
Why is Linux more secure than windows and any other OS
What is the difference between "su" and "su -" in Linux?
What is kernel-PAE in Linux?
What is swappiness and how do we change its value?
How to log iptables messages in different log file
What are the s and k scripts in the etc rcx.d directories
What is virtual memory, paging and swap space?
10 examples to help you understand top command usage in Unix/Linux