Operating System - Linux
1828636 Members
5887 Online
109983 Solutions
New Discussion

Script to delete few lines b4 and after a pattern

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Script to delete few lines b4 and after a pattern

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
For instance:
===========
..
..good line
This is good line
Do not delete this line
Garbage is here (DEL)
trash is here (DEL_
SUBJECT is HOWTO
No good info is here (DEL)
last line before the subject line ( DEL)
Good lines. Don't delete
Good lines. Don't delete
..good line
..good line
..good line
..good line
..good lines
Garbage is here (DEL)
trash is here (DEL_
SUBJECT is WHERE TO DO THIS
No good info is here (DEL)
last line before the subject line ( DEL)
...good lines
..good lines
...good line

====
How do you do that ? stumped.
Thanks in advance


good judgement comes from experience and experience comes from bad judgement.
2 REPLIES 2
Francisco J. Soler
Honored Contributor

Re: Script to delete few lines b4 and after a pattern

Hi Sam,

I send you a script, it has one limitation, the SUBJECT line dont must appear in the two first lines of the file.

To run the script type:

awk -f script.awk file > output

you can make a bash script with the awk script i've attached to. To do that create a text file with this lines:

#!/bin/bash
#
awk '
# here the code i've attached
' # don't forget this line is the end of the awk script.


To run the script make it executable and:

bash_script < file > output

hope this helps.

Frank.

Linux?. Yes, of course.
Claudio Cilloni
Honored Contributor
Solution

Re: Script to delete few lines b4 and after a pattern

I made a script to do this in 1 min., using Python.
the script is this:
------------------------------------------------------------
#! /usr/bin/python2

import sys

lines = sys.stdin.readlines()

for i in range(len(lines)):
for offset in (-2, -1, 1, 2):
ok = 1
try:
if lines[i + offset][:7] == 'SUBJECT':
ok = 0
break
except IndexError:
pass

if ok:
sys.stdout.write(lines[i])
--------------------------------------------------------------
save this script in the file 'linefilter.py' (the extension .py isn't necessary); give it the execute permission:

$ chmod +x linefilter.py

this script filters its stdin and print in stdout the same
lines filtered as you need:

$ cat file_to_be_filtered | ./linefilter.py >file_filtered

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.

the script in attached to this message, too.

hth
Claudio

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.