I have a file with following output:
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 2 test:
case id Name
====== ======== =================
BP 3 test:
case id Name
====== ======== =================
0 93 a_case-2-3-0
so, only "BP 0,1,3" have content, so what I want is, is it possible to dump 'BP 0 test','BP 1 test' and 'BP 3 test' only, just want to ingore the 'BP 2 test' because of no test case.
Thanks for your help.
The following ` sed ` command shows the use of ‘ c ‘ to replace everything after the match. Here, ‘ c ‘ indicates the change. The command will search the word ‘ present ‘ in the file and replace everything of the line with the text, ‘ This line is replaced ‘ if the word exists in any line of the file.
Patterns are encapsulated between delimiters, normally slash. Sed works line oriented. If you like to replace one (the first) foo with bar, above command is okay. To replace all, you need 'g' for globally. -n will not print by default, 1,5p means: print from line 1 to 5. This is equivalent. It will delete from line 6 to end.
The shell has a fair amount of built-in pattern matching functionality. This article is excerpted from the newly published book Beginning Portable Shell Scripting. Furthermore, many common Unix utilities, such as grep and sed, provide features for pattern matching.
The most used command is probably 's' for substitute. Others are 'd' for delete and 'p' for print. Patterns are encapsulated between delimiters, normally slash. Sed works line oriented. If you like to replace one (the first) foo with bar, above command is okay.
$ awk -F'\n' -v RS='' -v ORS='\n\n' 'NF>3' 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
While you could whack something together using smaller shell tools like [
and expr
, it'll be way easier to do this using awk
, which you'll usually find in any operating system that also includes grep
and sed
. :)
Here's a quick-and-dirty:
[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 ~]$
Note that this script assumes some things about your input data. If the conditions in the if
statement don't work for you, or if there's the possibility of multiple cases after a single test, this script will need to be adjusted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With