Pattern matching – Part 1

Pattern Matching Part 1 – grep

Today I am starting sessions on pattern matching utilities available on GNU / Linux systems.

grep command does pattern matching on files. One of the beauty of commands of GNU / Linux systems is that it reads from standard input and writes to standard output. This makes commands work in pipes.

When you have to search for particular word or pattern in files then grep is useful. grep is highly configurable.

Let us take a look at examples of grep.

1. Search for "Linux" word in cfg files from /boot/grub/
rahul@rahul-Aspire-4736Z:~$ grep "Linux" /boot/grub/*.cfg
set default="Advanced options for Ubuntu>Ubuntu, with Linux 3.8.0-34-generic"
    menuentry 'Ubuntu, with Linux 3.14.0-rc4+' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.0-rc4+-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.14.0-rc4+ ...'
    menuentry 'Ubuntu, with Linux 3.14.0-rc4+ (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.0-rc4+-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.14.0-rc4+ ...'
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+ ...'
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+ (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+ ...'
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+.old' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+.old-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+.old ...'
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+.old (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+.old-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+.old ...'
    menuentry 'Ubuntu, with Linux 3.8.0-34-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.8.0-34-generic-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.8.0-34-generic ...'
    menuentry 'Ubuntu, with Linux 3.8.0-34-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.8.0-34-generic-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.8.0-34-generic ...'
rahul@rahul-Aspire-4736Z:~$

2. Pattern matching done by grep is case sensitive by default. To ignore case 
use -i option.

rahul@rahul-Aspire-4736Z:~$ grep "Linux" /boot/grub/*.cfg -i 
set default="Advanced options for Ubuntu>Ubuntu, with Linux 3.8.0-34-generic"
### BEGIN /etc/grub.d/10_linux ###
        set linux_gfx_mode=keep
        set linux_gfx_mode=text
      set linux_gfx_mode=text
    set linux_gfx_mode=keep
  set linux_gfx_mode=text
export linux_gfx_mode
menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-a0dc6724-34b9-4738-a540-5e433d985aa7' {
    gfxmode $linux_gfx_mode
    linux    /boot/vmlinuz-3.14.0-rc4+ root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro   quiet splash $vt_handoff
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
    menuentry 'Ubuntu, with Linux 3.14.0-rc4+' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.0-rc4+-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        gfxmode $linux_gfx_mode
        echo    'Loading Linux 3.14.0-rc4+ ...'
        linux    /boot/vmlinuz-3.14.0-rc4+ root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro   quiet splash $vt_handoff
    menuentry 'Ubuntu, with Linux 3.14.0-rc4+ (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.0-rc4+-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.14.0-rc4+ ...'
        linux    /boot/vmlinuz-3.14.0-rc4+ root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro recovery nomodeset 
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        gfxmode $linux_gfx_mode
        echo    'Loading Linux 3.13.0-rc5+ ...'
        linux    /boot/vmlinuz-3.13.0-rc5+ root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro   quiet splash $vt_handoff
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+ (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+ ...'
        linux    /boot/vmlinuz-3.13.0-rc5+ root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro recovery nomodeset 
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+.old' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+.old-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        gfxmode $linux_gfx_mode
        echo    'Loading Linux 3.13.0-rc5+.old ...'
        linux    /boot/vmlinuz-3.13.0-rc5+.old root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro   quiet splash $vt_handoff
    menuentry 'Ubuntu, with Linux 3.13.0-rc5+.old (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.13.0-rc5+.old-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.13.0-rc5+.old ...'
        linux    /boot/vmlinuz-3.13.0-rc5+.old root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro recovery nomodeset 
    menuentry 'Ubuntu, with Linux 3.8.0-34-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.8.0-34-generic-advanced-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        gfxmode $linux_gfx_mode
        echo    'Loading Linux 3.8.0-34-generic ...'
        linux    /boot/vmlinuz-3.8.0-34-generic root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro   quiet splash $vt_handoff
    menuentry 'Ubuntu, with Linux 3.8.0-34-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.8.0-34-generic-recovery-a0dc6724-34b9-4738-a540-5e433d985aa7' {
        echo    'Loading Linux 3.8.0-34-generic ...'
        linux    /boot/vmlinuz-3.8.0-34-generic root=UUID=a0dc6724-34b9-4738-a540-5e433d985aa7 ro recovery nomodeset 
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
    linux16    /boot/memtest86+.bin
    linux16    /boot/memtest86+.bin console=ttyS0,115200n8

3. To get a count of matches done by grep use -c 

rahul@rahul-Aspire-4736Z:~$ grep "Linux" /boot/grub/*.cfg -c
17
rahul@rahul-Aspire-4736Z:~$ grep "Linux" /boot/grub/*.cfg -c -i 
45

For more information on different options see man page.

Post a Comment

You must be logged in to post a comment.
%d bloggers like this: