Match Pattern from file and Detele Matched as well as next 14lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match Pattern from file and Detele Matched as well as next 14lines
# 1  
Old 07-14-2009
Match Pattern from file and Detele Matched as well as next 14lines

Hi all,

Have a urgent query.....
I know to accept a string and search for that in a file and delete based on string.. But now i need to take input from a file, match file 1 and File 2 wen contents in file 1 are found in file 2 delete tht line and next 14 lines..

Example:

File1:
Code:
111
222
333
444

File2:
Code:
111
1
2
.
2
.
.
14lines
abc
qbc
222
....
...
14lines
cccc

Output:
Code:
abc
qbc
cccc

# 2  
Old 07-14-2009
Please share your code

Last edited by rakeshawasthi; 07-14-2009 at 02:03 AM..
# 3  
Old 07-14-2009
Hope this will resolve your problem

while read line
do
lineNum=$(grep -n "$line" file2|awk -F: '{print $1}')
endLine=`expr $lineNum + 14`
sed -i ''$lineNum', '$endLine' d' file2
done<file1
# 4  
Old 07-14-2009
@Sai21

Have you tested your code? does not work for me...

---------- Post updated at 11:21 AM ---------- Previous update was at 10:49 AM ----------

This was not as simple as it looked at first glance, coz of the fact that the file will change if we delete lines and so line nos will also subject to change.
Code:
cp file2 file3 #Backup file2
_lines_to_delete=`grep -n -f file1 file2 | cut -d":" -f1` #Lines to be deleted
for i in ${_lines_to_delete}
do
   _start=$i
   (( _end = $i + 14 ))
   awk '{if (NR>=a && NR <=b)print "d:"$0;else print}' a="${_start}" b="${_end}" file2 > _tempfile2 #Mark the lines as d
   mv _tempfile2 file2
done
awk -F":" '($1 !~ /d/) {print}' file2 # Print the remaining lines
mv file3 file2 #Restore file2

# 5  
Old 07-14-2009
cp file2 file2.bak # Just for safety
while read line ; do
lineNum=$(egrep -n "$line" file2 | sed 's/:.*//') # Only 1 line be there?
if [ "${lineNum}" > 0 ] ; then # Make sure it was found
till=$(expr ${lineNum} + 14)
sed "${lineNum},${till}d" file2 > out1.tmp # Except 1+14 write rest to temp file
mv out1.tmp file2
fi;
done < file1
cat file2 # Check your results if good

Last edited by edidataguy; 07-14-2009 at 04:30 AM.. Reason: Indented
# 6  
Old 07-14-2009
Try...
Code:
awk 'NR==FNR{a[$1]=1;next}a[$1]==1{c=15}--c<0' file1 file2 > file3

Tested...
Code:
$ head -999 file[123]
==> file1 <==
111
222
333
444

==> file2 <==
111
1
2
3
4
5
6
7
8
9
10
11
12
13
14
abc
qbc
222
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cccc

==> file3 <==
abc
qbc
cccc

# 7  
Old 07-14-2009
assume it is something like below perl script.

Code:
my %hash=(pat1=>3,pat2=>5,pat3=>2);
my $n;
while(<DATA>){
	chomp;
	if(exists $hash{$_}){
		$n=$hash{$_}+1;
	}
	if($n != 0){
		$n--;
		next;
	}
	print $_,"\n" if $n == 0;
}
__DATA__
1
pat1
a
b
c
2
3
pat2
a
b
c
c
d
4
5
6
pat3
g
h
7
8
9


output:
Code:
1
2
3
4
5
6
7
8
9

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

3. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Shell Programming and Scripting

ksh : need to get the 4 th line above and 2 nd below the matched pattern in the log file

I have a log file as given below 012/01/21 10:29:02 (111111) Processing Job '23_369468343464564' 2012/01/21 10:29:02 (111111) Making Job '23_369468343464564.0'... 2012/01/21 10:29:04 (111111) Jobnumber '23_369468343464564' was successful 2012/01/21 10:29:04 ... (12 Replies)
Discussion started by: rpm120
12 Replies

6. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

7. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

8. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

9. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question