LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2012, 01:21 PM   #1
LinuxChiq
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
Deleting a line that matches a string and also the line above it


This is what I have, I want to remove all lines that contain n/a and the line above it.


   <b>Earnings Releases - Confirmed</b></td>
<td align="right"><a target="_blank" href="earning.asp?date=20120629&client=cb&print=1"><img src="/format/cb/images/btn_print.gif" alt="Printer Friendly Version" width="62" height="16" hspace="10" border="0"></a></td>
<td align="left" width="*">Preliminary 2011 Berkeley Group Holdings PLC Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Preliminary 2012 Betfair Group PLC Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q1 2013 Constellation Brands, Inc. Earnings Release</td>
<td align="center">$ 0.39 </td>
<td align="left" width="*">Q4 2011 ESR SA Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q1 2013 Finish Line Earnings Release</td>
<td align="center">$ 0.23 </td>
<td align="left" width="*">Q1 2012 HPI AG Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Interim 2012 Hudaco Industries Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Full Year 2011/2012 IKB Deutsche Industriebank AG Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q3 2011/2012 Kappahl Holding AB (Publ) Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q2 2012 KB Home Earnings Release</td>
<td align="center">-$ 0.34 </td>
<td align="left" width="*">Preliminary 2011 Magforce Nanotechnologies AG Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Full Short Fiscal Year 2012 Medion AG Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q4 2012 Motorcar Parts of America, Inc. Earnings Release</td>
<td align="center">-$ 0.40 </td>
<td align="left" width="*">Q4 2011/2012 Paris Orleans SA Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q4 2011 Sts Group SA Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q4 2011 US Geothermal Inc Earnings Release</td>
<td align="center">n/a </td>
<td align="left" width="*">Q2 2012 ZENN Motor Co Inc Earnings Release</td>
<td align="center">n/a </td>
\n
Constellation Brands Inc


should become


   <b>Earnings Releases - Confirmed</b></td>
<td align="right"><a target="_blank" href="earning.asp?date=20120629&client=cb&print=1"><img src="/format/cb/images/btn_print.gif" alt="Printer Friendly Version" width="62" height="16" hspace="10" border="0"></a></td>
<td align="left" width="*">Q1 2013 Constellation Brands, Inc. Earnings Release</td>
<td align="center">$ 0.39 </td>
<td align="left" width="*">Q1 2013 Finish Line Earnings Release</td>
<td align="center">$ 0.23 </td>
<td align="left" width="*">Q2 2012 KB Home Earnings Release</td>
<td align="center">-$ 0.34 </td>
<td align="left" width="*">Q4 2012 Motorcar Parts of America, Inc. Earnings Release</td>
<td align="center">-$ 0.40 </td>
\n
Constellation Brands Inc


I've tried numerous SED commands such as sed '$!N;s/^\s*\n\(n\/a.*\)/\1/;P;D' but none have worked so far, does anyone have any suggestions for this?
 
Old 06-23-2012, 01:55 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Give this a try;
Code:
sed '{N;/n\/a/d}' infile
N (blue part) reads in the next line (both lines are now "one" in the buffer),
the grey part looks for lines containing n/a and if that is true the complete line is deleted (red d).

Test run on the input you provided:
Code:
$ sed '{N;/n\/a/d}' infile
<b>Earnings Releases - Confirmed</b></td>
<td align="right"><a target="_blank" href="earning.asp?date=20120629&client=cb&print=1"><img src="/format/cb/images/btn_print.gif" alt="Printer Friendly Version" width="62" height="16" hspace="10" border="0"></a></td>
<td align="left" width="*">Q1 2013 Constellation Brands, Inc. Earnings Release</td>
<td align="center">$ 0.39 </td>
<td align="left" width="*">Q1 2013 Finish Line Earnings Release</td>
<td align="center">$ 0.23 </td>
<td align="left" width="*">Q2 2012 KB Home Earnings Release</td>
<td align="center">-$ 0.34 </td>
<td align="left" width="*">Q4 2012 Motorcar Parts of America, Inc. Earnings Release</td>
<td align="center">-$ 0.40 </td>
\n
Constellation Brands Inc

Last edited by druuna; 06-23-2012 at 02:06 PM. Reason: found a simpler solution
 
1 members found this post helpful.
Old 06-23-2012, 06:29 PM   #3
LinuxChiq
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
wow, that was beautiful and succinct. Thank you!
 
  


Reply

Tags
bash, scripting, 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
print nth line after the line which matches the string cristalp Programming 7 10-27-2011 02:53 PM
[SOLVED] AWK print line only if next line matches a string wolverene13 Linux - Newbie 8 10-03-2011 04:32 PM
Deleting first line with unique string then all subsequent lines thereafter chriscbe Linux - Newbie 5 01-22-2011 12:58 AM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 04:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration