LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2009, 08:58 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Rep: Reputation: 28
sed match last x lines of a file


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?
 
Old 03-17-2009, 09:02 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Copy the lines to the Hold register ('H'), and have a sed command that tests for the end of the file and Gets the Hold register ('G').

Last edited by jschiwal; 03-17-2009 at 09:05 AM.
 
Old 03-17-2009, 11:03 AM   #3
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
solved

I was trying to append to the hold space, but I wasn't sure how many lines I needed. I solved it with this instead:

Code:
:t
/^title.$/,$ {
   $!{
   N;
   bt
   }
   p
}
That gave me the results I was looking for.

Last edited by bradvan; 03-17-2009 at 11:04 AM.
 
Old 03-17-2009, 11:15 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
sed '/^title.$/,$H;$G/' file
 
Old 03-17-2009, 11:18 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You are building up lines in the pattern space, where I built up the lines in the hold space.
 
Old 03-18-2009, 05:35 AM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
how to modify a little

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?

Thanks!
 
Old 03-18-2009, 05:53 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you have explained a lot, but i still cannot understand. show a sample of your grub.conf, show how you want it look in the end.
 
Old 03-18-2009, 06:24 AM   #8
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
example

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
after the script finishes.

Thanks!
 
Old 03-18-2009, 07:31 AM   #9
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
This adds the block and changes the 2 lines.
It modifies the original file so make a backup before testing it.
Code:
sed -n '/^title/,$ {
s/^title.*/& Single User Mode/
s/^     kernel.*/& s/
p}' file >> file
 
Old 03-18-2009, 07:57 AM   #10
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
great

Well, that gets me a lot closer. If I use:

Code:
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.

Thanks. This has been a big help!
 
Old 03-18-2009, 08:18 AM   #11
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
You don't need the tempory file.
'>> grub.conf' appends the output of sed to grub.conf
Code:
sed -n '/^title/,$ {
s/^title.*/& Single User Mode/
s/^     kernel.*/& s/
p}' grub.conf >> grub.conf

Last edited by Kenhelm; 03-18-2009 at 08:46 AM. Reason: Clarification
 
Old 03-18-2009, 09:19 AM   #12
bradvan
Member
 
Registered: Mar 2009
Posts: 167

Original Poster
Rep: Reputation: 28
Great! Thanks for the help!
 
Old 03-19-2009, 11:18 PM   #13
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge lines in a file using sed arobic Programming 8 01-20-2012 02:11 PM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
Insert and delete lines at the end of a file using sed DriveMeCrazy Programming 1 01-05-2007 01:45 AM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 08:34 AM


All times are GMT -5. The time now is 09:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration