• 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.

  • 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 ";"

    Deepak Prasad

    is a techie and an author who is still trying to survive in this IT generation with very little knowledge he has on Linux/Unix, VMware, SAN Storage, Automation, networking etc

    You can follow him on Facebook or Google+


    Do you also have something to share here?
    Join GoLinuxHub Team as an Author, Click here for more information
    sed: remove all leading and ending blank whitespace from a file sed: remove all leading and ending blank whitespace from a file Reviewed by Deepak Prasad on Sunday, June 25, 2017 Rating: 5

    No comments:

    Powered by Blogger.