Computing.Net > Forums > Unix > Script to delete lines from a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to delete lines from a file

Reply to Message Icon

Name: kangfu
Date: May 11, 2006 at 17:37:03 Pacific
OS: linux
CPU/Ram: intel
Comment:

hi,

I am trying to write a script to delete few lines from the existing files.

The requirement is as follows:
1. I need to grep for a pattern "abc" in a file and then grep for "xyz"
2. I need to delete all the lines (may be
2, 3 or more) between these 2 patterns.

what is the best possible way to do it. There ar multiple such files.
PLs let me know the possible solution.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 12, 2006 at 01:45:59 Pacific
Reply:

if you have python here is one possible solution using regular expression

import re
pat = re.compile('abc\n.*?xyz\n', re.DOTALL)
print re.sub(pat, 'abc\n' + 'xyz\n', open('yourfile.txt').read())


0

Response Number 2
Name: kangfu
Date: May 12, 2006 at 09:31:39 Pacific
Reply:

Hi, thanks for the reply.
I tried running the script but it does not delete the lines between the two patterns.


0

Response Number 3
Name: lchi2000g
Date: May 12, 2006 at 12:20:46 Pacific
Reply:

==> input file: input.txt

==> awk action file: action.txt

BEGIN {
first = 0
}
{
if ( first == 0 )
{
print $0
if ( $0 ~/abc/ )
{
first = 1
}
}
else
{
if ( $0 ~ /xyz/ )
{
print $0
first = 0
}
}
}

==> run:
$ awk -f action.txt input.txt
to get the output on the screen


Luke Chi


0

Response Number 4
Name: FishMonger
Date: May 12, 2006 at 20:50:04 Pacific
Reply:

Do you need to delete the lines that include the patterns or just the lines inbetween?


This perl command will do an inline edit of your file and delete the inbetween lines as well as the pattern matching lines.

perl -pi -e 'if(/abc/../xyz/){s/^.*$//s}' file.txt


This one will only delete the lines inbetween the matched the patterns.

perl -pi -e 'if(/abc/../xyz/){s/^.*$//s unless /(abc|xyz)/}' file.txt


0

Response Number 5
Name: FishMonger
Date: May 12, 2006 at 20:53:22 Pacific
Reply:

Here's another way of writting the first command.

perl -ni -e 'print unless(/abc/../xyz/)' file.txt


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Script to delete lines from a file

Script to Remove lines from a file www.computing.net/answers/unix/script-to-remove-lines-from-a-file/5268.html

Get the first line from a file www.computing.net/answers/unix/get-the-first-line-from-a-file/7981.html

Script to insert lines in a file www.computing.net/answers/unix/script-to-insert-lines-in-a-file/4992.html