LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH (grep / sed / awk): find string within a (line) range (http://www.linuxquestions.org/questions/linux-newbie-8/bash-grep-sed-awk-find-string-within-a-line-range-915416/)

dragonetti 11-24-2011 09:48 PM

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!

jschiwal 11-24-2011 09:59 PM

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.

dragonetti 11-24-2011 10:16 PM

Thanks [edit: also for the VERY fast response!!!] it worked!!!!!!!!!!!!!!!!!!!!


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