Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    5

    Can sed insert once?


    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 /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
    I want to insert this:

    Code:
    #Following lines added by kickstart\
    title   SDE - Windows XP\
            rootnoverify (hd0,0)\
            chainloader +1\
    #End of lines added by kickstart
    before the first 'title' keyword. Is there a way to do this with sed? My best effort so far is:

    Code:
    sed -n -e '0,/^password/p' -e '/^password/r grub.mod' -e '/^title/,$p' grub.conf
    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.

  2. #2
    Just 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.

  3. #3
    Just 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:

    #! /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
    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.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •