LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sed. Delete blank lines between two patterns (http://www.linuxquestions.org/questions/programming-9/sed-delete-blank-lines-between-two-patterns-822664/)

supersoni3 07-28-2010 12:35 PM

Sed. Delete blank lines between two patterns
 
Hello all

I am trying to delete any blank lines within two patterns e.g.

Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY



MM12 6MM
Pay Method : Crossed Cheque

The start of my pattern is "Cred Id" and the end is "Pay Method" and I want to delete the blank lines between county and post code.

I did find the code below but it doesn't seem to change anything:

sed -ne '/Cred Id/,/Pay Method/!bp' -e '/^$/b' -e :p -e p ll.out

I can get it to print just the range I'm interested in by doing sed -ne '/Cred Id/,/Pay Method/p' so I think that bit is right but despite doing some research I'm struggling a bit to understand the rest of the code.

Can anybody help or point me in the right direction?

Many thanks
Paul

druuna 07-28-2010 01:09 PM

Hi,

Is this what you are looking for:

sed '/Cred Id/,/Pay Method/{/^$/d}' infile

Hope this helps.

supersoni3 07-29-2010 05:23 AM

That's great. That does work thank you although I've since realised that they weren't blank lines in between the county and post code. The contains contained about 20 spaces instead!

druuna 07-29-2010 05:27 AM

Hi,

sed '/Cred Id/,/Pay Method/{/^[[:blank:]]*$/d}'

The above command will remove all lines that are empty, have spaces and/or tabs.

Hope this helps.

supersoni3 07-29-2010 11:23 AM

I got the "command garbled" message when I tried the above.

I managed to get it working by just removing spaces and also found that the space in the patterns needed to be escaped. In case anyone's interested I got it working by:

sed -e /Cred\ Id/,/Pay\ Method/!b -e '/^[ ]*$/d'


Thank you for putting me in the right direction druuna. Much appreciated!

Paul

druuna 07-29-2010 11:40 AM

Hi,

The command posted in post #4 works on this side (copied command form post #4):
Code:

$ cat infile
Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY

 

MM12 6MM
Pay Method : Crossed Cheque

Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY
       

 
MM12 6MM
Pay Method : Crossed Cheque

Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY

 
   
MM12 6MM
Pay Method : Crossed Cheque

$ sed '/Cred Id/,/Pay Method/{/^[[:blank:]]*$/d}' infile
Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY
MM12 6MM
Pay Method : Crossed Cheque

Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY
MM12 6MM
Pay Method : Crossed Cheque

Address: 53 HIGH STREET Cred Id :
MYTOWN
MYCOUNTY
MM12 6MM
Pay Method : Crossed Cheque

The infile has a mix of spaces, tabs and empty lines.

Instead of using [[:blank:]] you can also use [ <tab>] (that's a space and a tab). You can create the <tab> by pressing ctrl-v and then the tab key.

Hope this helps.


All times are GMT -5. The time now is 11:36 AM.