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

  • How to print or grep only the matched string or text or word or pattern in Linux using bash


    By default when we use grep without any switch it prints the entire line which matches the string we tried to grep for

    For eg
    I would like search for "ModLoad" in my rsyslog.conf file

    So when attempted without any switch or argument with grep
    # grep ModLoad /etc/rsyslog.conf
    $
    ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
    $ModLoad imjournal # provides access to the systemd journal
    $ModLoad imklog # reads kernel messages (the same are read from journald)
    $ModLoad immark  # provides --MARK-- message capability
    $ModLoad imudp
    $ModLoad imtcp






    So I get a whole bunch of stuff there but what if I would like to grep only the text "ModLoad" and nothing else
    # egrep -o ModLoad /etc/rsyslog.conf
    ModLoad
    ModLoad
    ModLoad
    ModLoad
    ModLoad
    ModLoad

    Here I have switched to egrep as now we plan on using regex which works well with egrep or you can use "grep -E"
    Here -o means "Print each match, but only the match, not the entire line."
    I hope the article was useful.

    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
    How to print or grep only the matched string or text or word or pattern in Linux using bash How to print or grep only the matched string or text or word or pattern in Linux using bash Reviewed by Deepak Prasad on Tuesday, December 12, 2017 Rating: 5

    No comments:

    Powered by Blogger.