HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Script to delete few lines b4 and after a pattern
Operating System - Linux
1828636
Members
5887
Online
109983
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2003 06:01 PM
06-28-2003 06:01 PM
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
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.
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2003 01:28 PM
06-29-2003 01:28 PM
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2003 01:33 PM
06-29-2003 01:33 PM
Solution
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.
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.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP