LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-28-2010, 11:35 AM   #1
supersoni3
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
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 -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
 
Old 07-28-2010, 12:09 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Is this what you are looking for:

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

Hope this helps.
 
1 members found this post helpful.
Old 07-29-2010, 04:23 AM   #3
supersoni3
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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!
 
Old 07-29-2010, 04:27 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
1 members found this post helpful.
Old 07-29-2010, 10:23 AM   #5
supersoni3
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 07-29-2010, 10:40 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
  


Reply

Tags
delete, lines, range, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM
Delete region of lines with sed freeindy Programming 7 09-21-2008 04:12 PM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
how to use sed to delete mutiple string patterns from apache access log matyu Programming 3 01-05-2008 11:42 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 01:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration