Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to match such pattern with shell or sed?

Tags:

shell

sed

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.

like image 759
user1110921 Avatar asked Apr 12 '12 04:04

user1110921


People also ask

How to replace everything after the match in sed command?

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.

How to replace a pattern with a bar in SED?

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.

How can I use pattern matching in the shell?

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.

What is the best way to replace in SED?

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.


2 Answers

$ 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
like image 141
kev Avatar answered Sep 22 '22 16:09

kev


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.

like image 33
ghoti Avatar answered Sep 21 '22 16:09

ghoti