LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 11-10-2009, 05:02 AM   #1
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Rep: Reputation: 0
traversing /boot/grub/grub.conf


Hi

I want to write a command, that will output the default "title" entry from the grub.conf file.

eg: cat /boot/grub/grub.conf | grep title

title Red Hat Enterprise Linux Server (2.6.18-128.1.6.el5)
title Red Hat Enterprise Linux Server (2.6.18-128.el5)

Now supposing the default=1 is set in the file, so I want the output "only" to be the second line.

Plz help with the pipelined command.
 
Old 11-10-2009, 06:21 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
grep for 'default' then grep title and use head/tail to limit the output to the desired line
 
Old 11-10-2009, 09:41 PM   #3
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Yes,

I already have the value of the 'default'
COUNT=`cat $ /boot/grub/grub.conf | grep default | awk 'BEGIN{FS="="};{print $2}'`

But I dont know how I can use this COUNT to get the corresponding 'title'. I tried using head/tail, but cudnt arrive at a consistent logic which will work for all 'default' values.

There are multiple enteries in my grub.conf file.
 
Old 11-10-2009, 09:51 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk
Code:
awk -F"=" '/^default/{val=$2}
/title/{  t[++c]=$0}
c>1{ print c, t[val+1] ;exit}' file

Last edited by ghostdog74; 11-10-2009 at 09:56 PM.
 
Old 11-11-2009, 01:30 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Code:
#!/bin/sh

DEFAULT=$(grep '^default' menu.lst |cut -f2 -d' ')

grep 'title' menu.lst |grep -v '#' |head -n +$DEFAULT |tail -n1
 
Old 11-11-2009, 01:44 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gnashley View Post
Code:
#!/bin/sh

DEFAULT=$(grep '^default' menu.lst |cut -f2 -d' ')

grep 'title' menu.lst |grep -v '#' |head -n +$DEFAULT |tail -n1
does your "default" line have "=" operator ? if yes, then cut is on wrong delimiter.
 
Old 11-11-2009, 03:45 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Mine doesn't. The OP might need to adjust some of the grep commands, depending on how his menu.lst is formatted.
For instance, in mine the line for 'default' has no spaces at the beginning of the line and the value is separated by a space. If he has tabs or '=' then he'd need to adjust that.
And, my lines with 'title' do have spaces at the beginning of the line.
Hopefully, the syntax and spacing is consistent in his menu.lst file. If not, then a more complex solution might be required. I just wanted to offer a non-awk, non-sed solution. Thw whole thing could be done with pure bash, but would be longer.
 
  


Reply



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
Why is /boot/grub/grub.conf and /etc/hosts not owned by any package shazia Linux - General 7 04-16-2012 01:32 AM
/boot/grub/grub.conf file has been deleted on rhel5 diwakars Linux - Newbie 5 12-03-2008 11:21 AM
Why /boot/grub/grub.conf is symlinked to /boot/grub/menu.lst raj_hcl1986@rediffma Linux - Newbie 2 10-19-2008 02:19 AM
Problems with /boot/grub/grub.conf not being read properly on reboots cmeyer Linux - Software 1 10-11-2008 06:34 PM
nano -w /boot/grub/grub.conf Tbagz Linux - Newbie 2 10-31-2005 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration