While you can hit something using smaller shell tools like [
and expr
, this will be easier to do with awk
, which you usually find on any operating system that also includes grep
and sed
. :)
It's quick and dirty here:
[ ghoti@pc ~]$ cat doit #!/usr/bin/awk -f /^BP/ { output=$0; getline; output=sprintf("%s\n%s", output, $0); getline; output=sprintf("%s\n%s", output, $0); getline; if (/[0-9]/) { output=sprintf("%s\n%s\n", output, $0); print output; } } [ ghoti@pc ~]$ ./doit input.txt BP 0 test: case id Name ====== ======== ================= 0 82 a_case-2-0-0 BP 1 test: case id Name ====== ======== ================= 0 86 a_case-2-1-0 BP 3 test: case id Name ====== ======== ================= 0 93 a_case-2-3-0 [ ghoti@pc ~]$
Please note that this script accepts some information about your input. If the conditions in the if
do not work for you, or if there is the possibility of several cases after one test, this script will need to be adjusted.
source share