This might be a common/easy task but I couldn't figure out from examples on the web or manuals of awk/sed/grep.
So, here is the scenario:
- There is an internal command line tool that prints out a multi-line result for each line in an input file.
- I have an input file with 500K lines.
- In the output of the tool, there is always a line which is like "src: /some/directory"
- I want to extract this line if and only if there is the specific string "foo" in the same output.
The number of rows between these lines might be different, so this question is somewhat related but not exactly what I'm trying to do. Match multiple regular expressions from a single file using awk
How can I do this using awk, sed or grep? I can do it using Python but I don't want to because I want to learn awk/sed and this might be a good example.
Here is what I tried with grep:
tool -inputfile | if grep "foo"; then grep "src: " ; fi > result.txt
This doesn't produce the result I expected, probably because of something related to buffering.
Trying with awk:
tool -inputfile | awk '{for (i=1;i<NF;i++) {if(match($i, "foo")) print ??? }}' > result.txt
How do I print the line that contains "src: " in this script?
Example outputs of the tool:
Output 1:
src: /usr/bin
param1: value1 value2
param2: "foo"
param3: "bar" "spam"
param4: "eggs" "spam" "spam"
Output 2:
src: /dev/null
param1: value1 value2
param2: "ham" "spam" "eggs"
So for these 2 cases, I am trying to extract just the 1st one, ie: src: /usr/bin