Matched multiple patterns that could be in a same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matched multiple patterns that could be in a same line
# 1  
Old 06-28-2014
Matched multiple patterns that could be in a same line

Hi,

I need help to match pattern started with "RW" in file 1 and with pattern in $1 in file 2 as follows:-


File 1
Code:
BH   /TOTAL=466(423); /POSITIVE=300(257); /UNKNOWN=25(25);
BH   /F_P=141(141); /F_N=136; /P=4;
CC   /TAX=!?; /MAX-R=2;
CC   /VER=2;
RW   P9610, AR_BSU , T; PAE25, AE_E57 , F; P24, AR_ELI , T;
RW   P498, A_KLE , P; QI77, BC_RBR , T; QJ9, BC_RIC , N;
RW   CV6, BCR_RPR , ?; Q686, BC_RTY , T; P061, CA_ELX , T;
RW   P2G4, CA_SATI , F; P2G3, CI_SAY , N; P12, CI_KLN , ?;
PO   POGY1

File 2
Code:
P9610     55.4.12.3     klskdls
QI77      3.221.12.1    abcd
P12       2.14.14.A     daswd
P24       1.4.56.43.1   dsadff
P2G4      2.A.3.6.3     azfddaf

The output file should look like this:-

Code:
P9610     55.4.12.3     klskdls     AR_BSU , T
QI77      3.221.12.1    abcd        BC_RBR , T
P12       2.14.14.A     daswd       CI_KLN , ?
P24       1.4.56.43.1   dsadff      AR_ELI , T
P2G4      2.A.3.6.3     azfddaf     CA_SATI , F

I don't know how should i do this in awk especially with multiple matched patterns could be in one line in file 1. really appreciate your kind help. thanks
# 2  
Old 06-28-2014
With over a 100 posts, and after getting our help on 33 threads you have started, what have you tried to do with awk to solve this problem? Where are you stuck?
# 3  
Old 06-28-2014
Hi Don Crugan,

i know how to match pattern when they are in columns and print the output furnished with patterns that i needed. But this one, i don't know how to deal with it because i could have a long line that contains more than 10 patterns. there are 3 things that i need to consider with this one, the "," space and ";". thanks.
# 4  
Old 06-28-2014
Try

Code:
$ awk -F'[ \t]+?[ ,;]' 'FNR==NR{A[$1]=$0;next}{$1=$1; $0=$0} $1=="RW" && ($2 in A){print A[$2],$3,$4}' file2 file1

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 06-28-2014
Hi akshay Hegde,


tried the code. It seems that it only print out matched pattern in 2nd column. for the given sample, the result as below:

Code:
P9610     55.4.12.3     klskdls AR_BSU T
P2G4      2.A.3.6.3     azfddaf CA_SATI F

it takes me some times to reply to you as i am still trying to play around with the codes to get the results that i wanted. thanks
# 6  
Old 06-28-2014
This should work
Code:
awk '
	FNR==NR{ 
		A[$1] = $0
		next
	}
	{
		gsub(/[[:space:]]+|,|;/," ")
		if($1 == "RW")
			for(i in A)
			  if(match($0,i))
				for(j=1;j<=NF;j++)
					if($j ~ i)
						print A[i], $(j+1),$(j+2)

	}
    ' OFS='\t' file2 file1

Code:
P9610     55.4.12.3     klskdls	AR_BSU	T
P24       1.4.56.43.1   dsadff	AR_ELI	T
QI77      3.221.12.1    abcd	BC_RBR	T
P2G4      2.A.3.6.3     azfddaf	CA_SATI	F
P12       2.14.14.A     daswd	CI_KLN	?

Format output according to your requirement in print statement
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 06-28-2014
Hi Akshay Hegde,

It worked great on my real data!! Thanks so much..owe u big time Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

3. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

4. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

5. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

6. Shell Programming and Scripting

Find matched patterns in multiple files

Hi, I need help to find matched patterns in 30 files residing in a folder simultaneously. All these files only contain 1 column. For example, File1 Gr_1 st-e34ss-11dd bt-wwd-fewq pt-wq02-ddpk pw-xsw17-aqpp Gr_2 srq-wy09-yyd9 sqq-fdfs-ffs9 Gr_3 etas-qqa-dfw ddw-ppls-qqw... (10 Replies)
Discussion started by: redse171
10 Replies

7. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

8. Shell Programming and Scripting

how to form Records[multiple line] between two known patterns

file contents looks like this : #START line1 of record1 line2 of record1 #END #START line1 of record2 line2 of record2 line3 of record2 #END #START line1 of record3 #END my question how should i make it a records between #START and #END . willl i be able to get the contents of the... (5 Replies)
Discussion started by: sathish92
5 Replies

9. UNIX for Dummies Questions & Answers

AWK: Multiple patterns per line

Hi there, We have been given a bit of coursework using awk on html pages. Without giving too much away and risking the wrath of the plagerism checks, I can say we need to deal with certain html elements. There may be several of these elements on one line. My question is, if there are more... (1 Reply)
Discussion started by: Plavixo
1 Replies

10. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies
Login or Register to Ask a Question