LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-24-2011, 10:48 PM   #1
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 18

Rep: Reputation: Disabled
BASH (grep / sed / awk): find string within a (line) range


I have this kind of akward string search.

Lets say I have this text file called "test.cnf":
Code:
[client]
port mysql    = 1234
port ftp      = 1235
etc...

[server]
port mysql    = 1234
port ftp      = 1235
etc...
I want to find the line number of "port mysql = 1234" under [client] and not under [server]

What it means is, I have to search only beneath [client] but not beneath [server].
There are 2 tabs between "port mysql" and "= 1234"

Both [client] and [server] parts are in one text file.

THANKS!
 
Old 11-24-2011, 10:59 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Code:
sed -n '/\[server\]/,/^$/{ /port mysql/p }'  test.cnf
The /\[server\]/,/^$/ selects the [server] section. The commands inside { .. } are only executed inside this range. You could have more than one command or even select a subrange if need be.

The mysql config files use a blank line between sections, which allows you to easily end select the end of the range.

Last edited by jschiwal; 11-24-2011 at 11:01 PM.
 
1 members found this post helpful.
Old 11-24-2011, 11:16 PM   #3
dragonetti
LQ Newbie
 
Registered: Sep 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks [edit: also for the VERY fast response!!!] it worked!!!!!!!!!!!!!!!!!!!!

Last edited by dragonetti; 11-24-2011 at 11:19 PM.
 
  


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
[SOLVED] bash- sed, awk, grep schneidz Programming 10 01-31-2011 09:56 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 02:34 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 09:04 PM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 09:59 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 09:55 AM


All times are GMT -5. The time now is 10:56 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