• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Did you find what you needed?
    Show your support and gratitude by making a contribution!!
    Life always offers you a second chance ... Its called tomorrow !!!

    Sunday, June 25, 2017

    sed: remove all leading and ending blank whitespace from a file

    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
       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, Openfiler
    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

    0 comments:

    Post a Comment