Reese Knowledgebase

Use sed or tr to remove parentheses from string

View Kristian Reese's profile on LinkedIn


If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 121
by: Reese K.
Posted: 10 Aug, 2013
Last updated: 10 Aug, 2013
Views: 1681

How to use sed or tr to remove parentheses from a string

In grub.conf, the kernel version is shown in the title line for each kernel version that's available for the system to boot.  I had a use case where I wanted to extract this information from this file within a pxeboot session, whereby common commands like 'uname -a' weren't available to otherwise garner this information more easily.

[root@vps1 ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /, eg.
#          root (hd0,0)
#          kernel /boot/vmlinuz-version ro root=/dev/mapper/mpsda1
#          initrd /boot/initrd-version.img
#boot=/dev/mapper/mpsda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title Virtuozzo (2.6.9-023stab053.2-smp)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.9-023stab053.2-smp ro root=/dev/mapper/mpsda1 debug max_scsi_luns=8
        initrd /boot/initrd-2.6.9-023stab053.2-smp-mpath.img
title Virtuozzo (2.6.9-023stab044.4-smp)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.9-023stab044.4-smp ro root=/dev/mapper/mpsda1 debug max_scsi_luns=8
        initrd /boot/initrd-2.6.9-023stab044.4-smp.img

Using tr

[root@vps1 ~]# cat /boot/grub/grub.conf | grep title | head -1 | awk '{ print $3 }' | tr -d '()'
2.6.9-023stab053.2-smp

Using sed

[root@vps1 ~]# cat /boot/grub/grub.conf | grep title | head -1 | awk '{ print $3 }' | sed 's:(::;s:)::'
2.6.9-023stab053.2-smp

This article was:   Helpful | Not Helpful
Also listed in
folder UNIX

Prev   Next
Sort IP Addresses in bash     How to expand an alias in a shell script

RSS