Results 1 to 3 of 3
I'm trying to edit a grub configuration file:
Code:
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 xxxxxxxxxxxxxxxxxxxxxxxxxxxx
title Scientific Linux SL (2.6.9-42.0.10.EL)
root (hd0,0)
kernel /vmlinuz-2.6.9-42.0.10.EL ro root=LABEL=/1 rhgb quiet
initrd ...
- 03-21-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 5
Can sed insert once?
I'm trying to edit a grub configuration file:
I want to insert this:Code:default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu password --md5 xxxxxxxxxxxxxxxxxxxxxxxxxxxx title Scientific Linux SL (2.6.9-42.0.10.EL) root (hd0,0) kernel /vmlinuz-2.6.9-42.0.10.EL ro root=LABEL=/1 rhgb quiet initrd /initrd-2.6.9-42.0.10.EL.img title Scientific Linux-up (2.6.9-42.0.3.EL) root (hd0,0) kernel /vmlinuz-2.6.9-42.0.3.EL ro root=LABEL=/1 rhgb quiet initrd /initrd-2.6.9-42.0.3.EL.img
before the first 'title' keyword. Is there a way to do this with sed? My best effort so far is:Code:#Following lines added by kickstart\ title SDE - Windows XP\ rootnoverify (hd0,0)\ chainloader +1\ #End of lines added by kickstart
where grub.mod is a file containing the insertion. Ideally I'd like to do this without relying on the occurrence of 'password' - i.e. just make the insertion before the first occurrence of title.Code:sed -n -e '0,/^password/p' -e '/^password/r grub.mod' -e '/^title/,$p' grub.conf
- 03-30-2007 #2Just Joined!
- Join Date
- Mar 2007
- Posts
- 16
Hi mwhidby,
Since you are looking to doing something with just one occurence, it would be easier to use ed instead of sed. sed is more useful when changing multiple occurences. Create an ed script file with the following:
/title/
i
#Following lines added by kickstart\
title SDE - Windows XP\
rootnoverify (hd0,0)\
chainloader +1\
#End of lines added by kickstart
.
w (follow with new file name if you do not want to change the original)
q
Use ed and the script file:
ed -s grub.conf < scriptfilename > /dev/null
This example will modify the grub.conf file directly if there is no argument following the "w" in the script file. You could make grub.mod and use it or use the "w" in the script file as noted to save the file as grub.mod.
- 03-30-2007 #3Just Joined!
- Join Date
- Mar 2007
- Posts
- 16
Hi mwhidby,
If you do need to use sed, one way would be to use a sed script that also determines the specific line number to change:
Note that you need to use \\ at the end of lines where a newline is needed. Also, remove the extra space if lines are not concatenated.#! /bin/sh
linenum=`grep -m 1 -n "title" grub.conf | cut -f1 -d:`
sed "${linenum}i\
#Following lines added by kickstart\\
title SDE - Windows XP \
rootnoverify (hd0,0) \
chainloader +1 \\
#End of lines added by kickstart\
" grub.conf > grub.mod


Reply With Quote