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
# 8  
Old 06-28-2014
To get data in the format that was specified in the 1st post in this thread, you could try:
Code:
awk -F '   *| *; *' '
# Input field separator is (two or more spaces) or (semicolon and any adjacent
# [before or after the semicolon] spaces).
FNR == NR {
	# Process lines from 1st file...
	# Skip to next line if 1st field is not "RW"...
	if($1 != "RW")
		next
	# We have an RW line; grab keys and values from remaining semicolon
	# separated fields...
	for(i = 2; i < NF; i++) {
		# printf("line:%s, field:%d, %s\n", NR, i, $i)
		# The key comes before the 1st comma...
		if(match($i, /, */)) {
			# value follows spaces after the comma.
			k[s = substr($i, 1, RSTART - 1)] = \
				substr($i, RSTART + RLENGTH - 1)
			# printf("k[%s]=%s\n", s, k[s])
		}
	}
	next
}
{	# Process lines from 2nd file...
	# Print line along with value corresponding to key found in field 1...
	printf("%-34s %s\n", $0, k[$1])
}' File\ [12]

which produces the output:
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

(note the commas between the last two fields) from the sample input files.

Moderator's Comments:
Mod Comment In the future, we'll expect you to show us that you have actually made an attempt to solve your problem with what you have learned by following these forums for 3.5 years. Just saying "there are things I need to consider" makes it look like you want to use this site as a free programming department. That is not why we volunteer our time here; we want to help you learn how to write your own code. When you run into problems, we're happy to help you learn how to get around them.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 06-28-2014
Hi Don Crugan,

Thanks so much for your codes. i will try it out and give feedback here. Normally, i will take some times to respond as i am trying to understand the codes and check the results. my data normally more than 10 thousands lines.

It was my bad not to show u my effort. in my previous post, i did show what i tried before getting help here unless i have no idea how to do it at all. in this case, i seriously don't know how to find the matched patterns in a line. i know how to arrange the results. Thanks so much for your reminder. I will keep it in mind. Smilie
# 10  
Old 06-28-2014
I'm glad that you're trying to learn from the code you've been given. Smilie

If there is anything you don't understand, ask questions. The volunteers here are happy to help you learn by answering questions you may have about the code they provide.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 07-01-2014
Hi Don Crugan,

just for the feedback. The codes did work well. It is a little bit complicated for me to understand but the comments provided with the codes really a big help. Thanks again. 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