LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Deleting a line that matches a string and also the line above it (http://www.linuxquestions.org/questions/linux-newbie-8/deleting-a-line-that-matches-a-string-and-also-the-line-above-it-4175413009/)

LinuxChiq 06-23-2012 01:21 PM

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.


&#160;&#160;&#160;<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&#160;</td>
<td align="left" width="*">Preliminary 2012 Betfair Group PLC Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q1 2013 Constellation Brands, Inc. Earnings Release</td>
<td align="center">$ 0.39&#160;</td>
<td align="left" width="*">Q4 2011 ESR SA Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q1 2013 Finish Line Earnings Release</td>
<td align="center">$ 0.23&#160;</td>
<td align="left" width="*">Q1 2012 HPI AG Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Interim 2012 Hudaco Industries Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Full Year 2011/2012 IKB Deutsche Industriebank AG Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q3 2011/2012 Kappahl Holding AB (Publ) Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q2 2012 KB Home Earnings Release</td>
<td align="center">-$ 0.34&#160;</td>
<td align="left" width="*">Preliminary 2011 Magforce Nanotechnologies AG Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Full Short Fiscal Year 2012 Medion AG Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q4 2012 Motorcar Parts of America, Inc. Earnings Release</td>
<td align="center">-$ 0.40&#160;</td>
<td align="left" width="*">Q4 2011/2012 Paris Orleans SA Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q4 2011 Sts Group SA Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q4 2011 US Geothermal Inc Earnings Release</td>
<td align="center">n/a&#160;</td>
<td align="left" width="*">Q2 2012 ZENN Motor Co Inc Earnings Release</td>
<td align="center">n/a&#160;</td>
\n
Constellation Brands Inc


should become


&#160;&#160;&#160;<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&#160;</td>
<td align="left" width="*">Q1 2013 Finish Line Earnings Release</td>
<td align="center">$ 0.23&#160;</td>
<td align="left" width="*">Q2 2012 KB Home Earnings Release</td>
<td align="center">-$ 0.34&#160;</td>
<td align="left" width="*">Q4 2012 Motorcar Parts of America, Inc. Earnings Release</td>
<td align="center">-$ 0.40&#160;</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?

druuna 06-23-2012 01:55 PM

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


LinuxChiq 06-23-2012 06:29 PM

wow, that was beautiful and succinct. Thank you!


All times are GMT -5. The time now is 06:14 AM.