<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Script to delete few lines b4 and after a pattern in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010552#M5401</link>
    <description>Hi Sam,&lt;BR /&gt;&lt;BR /&gt;I send you a script, it has one limitation, the SUBJECT line dont must appear in the two first lines of the file.&lt;BR /&gt;&lt;BR /&gt;To run the script type:&lt;BR /&gt;&lt;BR /&gt;awk -f script.awk file &amp;gt; output&lt;BR /&gt;&lt;BR /&gt;you can make a bash script with the awk script i've attached to. To do that create a text file with this lines:&lt;BR /&gt;&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;#&lt;BR /&gt;awk '&lt;BR /&gt;# here the code i've attached&lt;BR /&gt;'  # don't forget this line is the end of the awk script.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;To run the script make it executable and:&lt;BR /&gt;&lt;BR /&gt;bash_script &amp;lt; file &amp;gt; output&lt;BR /&gt;&lt;BR /&gt;hope this helps.&lt;BR /&gt;&lt;BR /&gt;Frank.&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Sun, 29 Jun 2003 20:28:50 GMT</pubDate>
    <dc:creator>Francisco J. Soler</dc:creator>
    <dc:date>2003-06-29T20:28:50Z</dc:date>
    <item>
      <title>Script to delete few lines b4 and after a pattern</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010551#M5400</link>
      <description>Hate to do this manually. Have a file and I need to delete 2 lines prior to the the line that starts with "SUBJECT" and 2 lines after it (and keep the line that begins with word SUBJECT). Have many occurences of a line that start with SUBJECT&lt;BR /&gt; For instance:&lt;BR /&gt;===========&lt;BR /&gt;..&lt;BR /&gt;..good line&lt;BR /&gt;This is good line&lt;BR /&gt;Do not delete this line&lt;BR /&gt;Garbage is here (DEL)&lt;BR /&gt;trash is here (DEL_&lt;BR /&gt;SUBJECT is HOWTO&lt;BR /&gt;No good info is here (DEL)&lt;BR /&gt;last line before the subject line ( DEL)&lt;BR /&gt;Good lines. Don't delete&lt;BR /&gt;Good lines. Don't delete&lt;BR /&gt;..good line&lt;BR /&gt;..good line&lt;BR /&gt;..good line&lt;BR /&gt;..good line&lt;BR /&gt;..good lines&lt;BR /&gt;Garbage is here (DEL)&lt;BR /&gt;trash is here (DEL_&lt;BR /&gt;SUBJECT is WHERE TO DO THIS&lt;BR /&gt;No good info is here (DEL)&lt;BR /&gt;last line before the subject line ( DEL)&lt;BR /&gt;...good lines&lt;BR /&gt;..good lines&lt;BR /&gt;...good line&lt;BR /&gt;&lt;BR /&gt;====&lt;BR /&gt;How do you do that ? stumped.&lt;BR /&gt;Thanks in advance&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 29 Jun 2003 01:01:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010551#M5400</guid>
      <dc:creator>Sammy_2</dc:creator>
      <dc:date>2003-06-29T01:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: Script to delete few lines b4 and after a pattern</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010552#M5401</link>
      <description>Hi Sam,&lt;BR /&gt;&lt;BR /&gt;I send you a script, it has one limitation, the SUBJECT line dont must appear in the two first lines of the file.&lt;BR /&gt;&lt;BR /&gt;To run the script type:&lt;BR /&gt;&lt;BR /&gt;awk -f script.awk file &amp;gt; output&lt;BR /&gt;&lt;BR /&gt;you can make a bash script with the awk script i've attached to. To do that create a text file with this lines:&lt;BR /&gt;&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;#&lt;BR /&gt;awk '&lt;BR /&gt;# here the code i've attached&lt;BR /&gt;'  # don't forget this line is the end of the awk script.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;To run the script make it executable and:&lt;BR /&gt;&lt;BR /&gt;bash_script &amp;lt; file &amp;gt; output&lt;BR /&gt;&lt;BR /&gt;hope this helps.&lt;BR /&gt;&lt;BR /&gt;Frank.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 29 Jun 2003 20:28:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010552#M5401</guid>
      <dc:creator>Francisco J. Soler</dc:creator>
      <dc:date>2003-06-29T20:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: Script to delete few lines b4 and after a pattern</title>
      <link>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010553#M5402</link>
      <description>I made a script to do this in 1 min., using Python.&lt;BR /&gt;the script is this:&lt;BR /&gt;------------------------------------------------------------&lt;BR /&gt;#! /usr/bin/python2&lt;BR /&gt;&lt;BR /&gt;import sys&lt;BR /&gt;&lt;BR /&gt;lines = sys.stdin.readlines()&lt;BR /&gt;&lt;BR /&gt;for i in range(len(lines)):&lt;BR /&gt;    for offset in (-2, -1, 1, 2):&lt;BR /&gt;        ok = 1&lt;BR /&gt;        try:&lt;BR /&gt;            if lines[i + offset][:7] == 'SUBJECT':&lt;BR /&gt;                ok = 0&lt;BR /&gt;                break&lt;BR /&gt;        except IndexError:&lt;BR /&gt;            pass&lt;BR /&gt;&lt;BR /&gt;    if ok:&lt;BR /&gt;        sys.stdout.write(lines[i])&lt;BR /&gt;--------------------------------------------------------------&lt;BR /&gt;save this script in the file 'linefilter.py' (the extension .py isn't necessary); give it the execute permission:&lt;BR /&gt;&lt;BR /&gt;$ chmod +x linefilter.py&lt;BR /&gt;&lt;BR /&gt;this script filters its stdin and print in stdout the same&lt;BR /&gt;lines filtered as you need:&lt;BR /&gt;&lt;BR /&gt;$ cat file_to_be_filtered | ./linefilter.py &amp;gt;file_filtered&lt;BR /&gt;&lt;BR /&gt;I hope that python is installed in you linux system (usually it is). maybe you need to change the name of the python interpreter in '/usr/bin/python' (instead of python2) or give the full path to it if it isn't installed in /usr/bin.&lt;BR /&gt;&lt;BR /&gt;the script in attached to this message, too.&lt;BR /&gt;&lt;BR /&gt;hth&lt;BR /&gt;Claudio&lt;BR /&gt;&lt;BR /&gt;p.s.: I think it isn't too difficult to do this using a shell script (more standard!), but python is really great when manipulating text ;-). Perl should be good, too.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 29 Jun 2003 20:33:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/script-to-delete-few-lines-b4-and-after-a-pattern/m-p/3010553#M5402</guid>
      <dc:creator>Claudio Cilloni</dc:creator>
      <dc:date>2003-06-29T20:33:15Z</dc:date>
    </item>
  </channel>
</rss>

