• 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: Ignore whitespace while matching a pattern

    Suppose you have a set of words or line which want to match and perform some activity but you do not know the no of whitespace between each word or string.

    For eg I have a file /tmp/file with below content
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuset
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct

    I would like to grep for "Initializing cgroup subsys cpuset"
    but I do not want to risk the grep with the spaces between every word

    If I do a normal search and replace like below
    # sed 's/Initializing cgroup subsys cpuset/NEW CONTENT/g' /tmp/file
    Dec 14 13:29:13 cc01-pgd-002a kernel: NEW CONTENT
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct






    It worked as expected but what if there were some extra spaces between the words like below
    # cat /tmp/file
    Dec 14 13:29:13 cc01-pgd-002a kernel:
    Initializing   cgroup subsys   cpuset
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct

    and trying the same command as above
    # sed 's/Initializing cgroup subsys cpuset/NEW CONTENT/g' /tmp/file
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing   cgroup subsys   cpuset
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct

    It didn't worked.

    So we have to try to ignore these whitespaces so that we have a perfect match
    # sed 's/Initializing\s*cgroup\s*subsys\s*cpuset/NEW CONTENT/g' /tmp/file
    Dec 14 13:29:13 cc01-pgd-002a kernel: NEW CONTENT
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct

    It worked so below is what you have to use
    \s   This is equivalent to [[:blank:]]
    *    means 0 or higher

    So search for all whitespace (0 or higher) when used
    # sed 's/Initializing[[:blank:]]*cgroup[[:blank:]]*subsys[[:blank:]]*cpuset/NEW CONTENT/g' /tmp/file
    Dec 14 13:29:13 cc01-pgd-002a kernel: NEW CONTENT
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpu
    Dec 14 13:29:13 cc01-pgd-002a kernel: Initializing cgroup subsys cpuacct


    Follow the below links for more tutorials



    Deepak Prasad

    Deepak Prasad is an anonymous 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?

    Become an author and be a part of our GoLinuxHub Team, Click here for more information
    sed: Ignore whitespace while matching a pattern sed: Ignore whitespace while matching a pattern Reviewed by Deepak Prasad on Sunday, December 17, 2017 Rating: 5

    No comments:

    Powered by Blogger.