Register
It is currently Fri Dec 19, 2014 4:22 am

delete X lines after a list of matches


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu May 13, 2010 12:28 pm   

Joined: Thu May 13, 2010 10:47 am
Posts: 1
I have two files:
file1:
Code:
matchexpression1
matchexpression2
matchexpression3

file2:
Code:
random text matchexpression1 random text
random text
random text
random text
random text matchexpression2 random text
random text
random text
random text
random text matchexpression3 random text
random text
random text
random text

I'd like to iterate through file1 line-by-line and delete all lines that match as well as the next X lines below it in file2. So, if I wanted to delete the matching line of each expression in file1 as well as the next _two_ lines I'd be left with a file3 that looks like:
Code:
random text
random text
random text

Does anyone know how to accomplish this? Thx.


Top
 Profile  
 PostPosted: Thu May 13, 2010 1:43 pm   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
I'm not sure, but you can try this:

Code:
#!/bin/sh


NUMBER=$(wc $2 | awk '{ print $1}') #Save number of lines of the pattern file

cp $1 /tmp/modified1.$$          # Make a tmp file for the one that will be modified.

for (( i=1; i<=$NUMBER;i++)) do   # Loop for all lines of the pattern file             
PATTERN=$(sed -n "$i p" $2)       # get the pattern 

sed -e "/$PATTERN/ {                        # Mathes the pattern?
N                                           # Append first
N                                           # Append second
d                                           # Delete all
}" /tmp/modified1.$$  >> /tmp/modified2.$$  # Save in a second tmp file

mv -f /tmp/modified2.$$ /tmp/modified1.$$   # Update the first tmp file

done

mv -f /tmp/modified1.$$ file3


Of course you must run this as

Code:
script_file file2 file1

Where "script_file" is the file with the content above and with the execution bit set.


Top
 Profile  
 PostPosted: Wed May 19, 2010 9:49 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Here's an example :) it returns the row number where a match occured, then you can use SED to cut out the specifics

Code:
old_IFS=$IFS
IFS="
"
for a in $(cat -n file); do
    for i in $(cat match); do
        echo $a | grep $i | awk {'print $1'}
    done
done
IFS=$old_IFS


What it does is just number the lines of the file to match and then "greps" after the expression. When a match is found it just prints the row number (produced by "cat -n")

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu May 20, 2010 2:58 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You can also use just
Code:
sed '/expression1\|expression2/,+Xd'

where X is the number of lines to delete. This would not work if another match occurs on one of the deleted lines.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP