jl487
April 23, 2012, 1:43pm
1
Hello,
I have a text file like the one found below and wouild like the grab the certain lines after the searched phrase. For example, I'd like to look up "Hello" and once I find the "Hello" section, grab the lines that contain "Text" and stops at the next section.
Input.txt
Example Hello
Filler A
Filler B
Filler C
Text Bon Jour
Text Salut
Text Hola
Filler Z
Example Bye
Filler A1
....
...
desired output:
Text Bon Jour
Text Salut
Text Hola
jl487
April 23, 2012, 1:51pm
3
corona688:
What's your system?
I'm running on an AIX machine.
sed -n '/Example Hello/,/Example/s/Text.*/&/p' myFile
1 Like
jl487
April 23, 2012, 2:43pm
5
wow, didn't realize sed can be used like that! thanks!
---------- Post updated at 02:43 PM ---------- Previous update was at 02:43 PM ----------
what would the sed syntax look like if I wanted to reverse the order?
So if I was looking for "Text Bon Jour" my ouput would be "Example Hello"?
Please provide a representative data sample and a desired output.
jl487
April 23, 2012, 3:08pm
7
well, I guess the input file would be the same:
Example Hello
Filler A
Filler B
Filler C
Text Bon Jour
Text Salut
Text Hola
Filler Z
Example Bye
Filler A1
....
...
and if I did a query for "Text Bon Jour" or "Text Hola", my ouput would be the following:
Example Hello
nawk -v p='Text Bon Jour' '/^Example/{e=$0;next} $0~p{print e}' myFile
jl487
April 24, 2012, 11:10am
9
can this be modified so that if the p value is not found, then it will just echo out the p value.
For example, query for "Wow" will echo out wow since it's not in the input file. A querey for "Text Bon Jour" will echo "Example Hello"