ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to find the correct sed syntax to match from a given pattern to the end of the file and then append that pattern to the end of the file. I was trying:
sed -e '/^title.*$/,$ p' /boot/grub/grub.conf
but that prints each line right after its original occurrence. I want to match the block and then append it to the end of the file. Any suggestions?
What I am trying to do is write a script to run post install to modify grub.conf to add a single user mode boot option. Since this is a new install, there will only be a single title line followed by some number of supporting lines. I want to copy that block, modify the title and kernel lines and paste it to the end. I can now copy the block, but then I'm not certain how to modify only the second title and kernel lines. I've thought about trying to modify each line as I hit it and put the modified line in the hold space, but I am not having much luck there either. I'm sure someone must have done this already. Any suggestions?
typically, after doing a RHEL vs 5 install, the grub.conf looks similar to:
Code:
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-8.e15)
root (hd0,0)
kernel /boot/vmlinux-2.6.18-8.e15 ro root=LABEL=/ rhgb quite crashkernel=128M@16M
initrd /boot/initrd-2.6.18-8.e15.img
I would like it to look like:
Code:
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-8.e15)
root (hd0,0)
kernel /boot/vmlinux-2.6.18-8.e15 ro root=LABEL=/ rhgb quite crashkernel=128M@16M
initrd /boot/initrd-2.6.18-8.e15.img
title Red Hat Enterprise Linux Server (2.6.18-8.e15) Single User Mode
root (hd0,0)
kernel /boot/vmlinux-2.6.18-8.e15 ro root=LABEL=/ rhgb quite crashkernel=128M@16M s
initrd /boot/initrd-2.6.18-8.e15.img
sed -n '/^title/,$ {s/^title.*$/& Single User Mode/;s/\tkernel.*$/& s/;p}' grub.conf
it gives:
Code:
title Red Hat Enterprise Linux Server (2.6.18-8.e15) Single User Mode
root (hd0,0)
kernel /boot/vmlinux-2.6.18-8.e15 ro root=LABEL=/ rhgb quite crashkernel=128M@16M s
initrd /boot/initrd-2.6.18-8.e15.img
I can just save that to a temporary file and then tack it on to the end of grub.conf.
The -i option is often used to edit file inplace. Given the importance of the grub.conf file, manually adding it after inspection or redirecting the output to a temporary file and inspecting it before replacing the grub.conf (or menu.lst) file may be a good idea.
For example, what happens in the future if the install produces 2 stanzas. You are using a range that would cover both stanzas instead of just one.
It would be nice if there was a blank line after each stanza. Then a range could be:
/^title/,/^$/
Using /<pattern 1>/,/^$/ is very common for selecting ranges. The format of the lspci output file is a common example.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.