Reputation: 331
I would like to parse a text like that:
# cmd: Text_finder --outdir 1
# No Text found
# cmd: Text_finder --outdir 2
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
Text_01 tt 002 454 517 1 1.9e-05 No
Text_01 tt_c7 003 1276 1362 1 2.2e-08 No
# cmd: Text_finder --outdir 3
# No Text found
# cmd: Text_finder --outdir 4
# No Text found
# cmd: Text_finder --outdir 5
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
in order to retrieve lines starting with "Text_01" (results), "ID_Text" (headline) but also line before "ID_Text" (starting with "# cmd:" but also common to the line with no result I want to get ride off).
I have tried to combine and execute these lines:
#get lines with "ID_text" and "text_01"
grep -P "ID_text|text_01" result.txt > positive-results.txt;
#get line before line with "ID_text"
grep -B1 "ID_text" result.txt > positive-results.txt;
but I cannot get a file like that:
# cmd: Text_finder --outdir 2
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
Text_01 tt 002 454 517 1 1.9e-05 No
Text_01 tt_c7 003 1276 1362 1 2.2e-08 No
# cmd: Text_finder --outdir 5
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
Do you have a smart way to do that?
Thanks a lot
Upvotes: 0
Views: 84
Reputation: 37464
$ grep --no-group-separator -B 1 "^\(Text_01\|ID_Text\)" file
Output:
# cmd: Text_finder --outdir 2
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
Text_01 tt 002 454 517 1 1.9e-05 No
Text_01 tt_c7 003 1276 1362 1 2.2e-08 No
# cmd: Text_finder --outdir 5
ID_Text ID element pos_beg pos_end model type default distance
Text_01 tt 001 38 108 1 6.3e-05 No
man grep
:
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches.
Interestingly my man page has no explanation for --no-group-separator
but try above once without it and you'll see.
Upvotes: 2