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

Script to delete lines from a file

Reply to Message Icon

Original Message
Name: kangfu
Date: May 11, 2006 at 17:37:03 Pacific
Subject: Script to delete lines from a file
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.


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: May 12, 2006 at 01:45:59 Pacific
Subject: Script to delete lines from a file
Reply: (edit)

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())


Report Offensive Follow Up For Removal

Response Number 2
Name: kangfu
Date: May 12, 2006 at 09:31:39 Pacific
Subject: Script to delete lines from a file
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 3
Name: lchi2000g
Date: May 12, 2006 at 12:20:46 Pacific
Subject: Script to delete lines from a file
Reply: (edit)

==> 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


Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: May 12, 2006 at 20:50:04 Pacific
Subject: Script to delete lines from a file
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 5
Name: FishMonger
Date: May 12, 2006 at 20:53:22 Pacific
Subject: Script to delete lines from a file
Reply: (edit)

Here's another way of writting the first command.

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Script to delete lines from a file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge