AWK:- matching pattern search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK:- matching pattern search
# 1  
Old 04-03-2011
AWK:- matching pattern search

Dear Friends,
I have a flat file. To pick certain details we have written an awk where we are facing difficulty.

Sample of flat file.


line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14 (Matching pattern "Lkm_i-lnr:"can be either here)
line 15 (Matching pattern "Lkm_i-lnr:" can be either here)
line 16 (Matching pattern "Lkm_i-lnr:" can be or here)
line 17
line 18
line 19
line 20


To pick details till line 13 we are using following code.

Code:
#earlier script is truncated. 
if (startRead==11) {
buyFrq=substr($0,1,20)
gsub(" ","",buyFrq)
size=substr($0,20,30)
gsub(" ","",size)
incom=substr($0,50,20)
gsub(" ","",incom)
startRead=12
print buyFrq
print rtOint
print incom
next
}
if (startRead==12) {
startRead=13
next
}
if (startRead==13) {
mRp=match($0,"LKM_lp-gloss:")
if ( match($0,"LKM_lp-gloss:") != 0 ) {
mrp1=substr($0,mRp+7,10)
startRead=14
}
else {
startRead=14
}
print mrp1
next
}

}
END {
}


But from line 14, position of matching pattern can change between line 14 and line 16. The code written by us is working fine for capturing details till line 13 (As matching pattern's line position is fixed).

Request you to guide me to write remaining awk with similar logic (or in a simple to understand way) that we have used for ealier lines for our better understanding and better manageability point of view.

Thank you in advance
Waiting for your reply.
Anu.

Last edited by anushree.a; 04-03-2011 at 06:02 AM..
# 2  
Old 04-03-2011
not sure what you need here: remove "Lkm_i-lnr:" where ever it may occur in lines 14-16?
# 3  
Old 04-03-2011
Dear Kato,

No, we dont want to "Remove" it.
Its a product code (lkm_i-lnrSmilie and thats a search pattern.

wherever we find this product code (either in line 14/15/16) we want to grep and print next 10 characters written next to it.
# 4  
Old 04-03-2011
Use index function in awk to locate your product code and then substr to get the value.
# 5  
Old 04-03-2011
Like this?
Code:
awk '/Lkm_i-lnr:/{split($0, prod, "Lkm_i-lnr:");print substr(prod[2],0,10)}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Pattern matching using awk

Hi I am trying to find a pattern match with column one containing 3 numbers. input file tmp.lst abcd456|1|23123|123123|23423 kumadff|a|dadfadf|adfd|adfadfadf xxxd999|d|adfdfs|adfadf|adfdasfadf admin|a|dafdf|adfadfa||| output file tmp4.lst abcd456|1|23123|123123|23423... (3 Replies)
Discussion started by: vamsekumar
3 Replies

3. Shell Programming and Scripting

awk pattern matching

can somebody provide me with some ksh code that will return true if my the contents in my variable match anyone of these strings ORA|ERROR|SP2 variable="Error:ORA-01017: Invalid username/password; logon denied\nSP2-0640:Not connected" I tried this and it does not seem to work for me ... (3 Replies)
Discussion started by: BeefStu
3 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

6. UNIX for Dummies Questions & Answers

awk - pattern matching?

Hello all, I am trying to sort thru a database and print all the customers whose first names are only four characters. I just want to pull the first name only from the database. the database records appear like this in file: Mike Harrington:(510) 548-1278:250:100:175; first is name Mike... (4 Replies)
Discussion started by: citizencro
4 Replies

7. Shell Programming and Scripting

AWK pattern matching

Hi, How can I tell awk to print all lines/columns if column number 5 contains the word Monday? I have tried nawk -F, '$5==Monday' OFS=, myfile > outputfile but that doesn't work (I am a newb!!) Thanks, (7 Replies)
Discussion started by: keenboy100
7 Replies

8. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

9. Shell Programming and Scripting

pattern matching using awk.

Dear Team, How do we match two patterns on the same line using awk?Are there any logical operators which i could use in awk like awk '\gokul && chennai\' <filename> Eg: Input file: gokul,10/11/1986,coimbatore. gokul,10/11/1986,bangalore. gokul,12/04/2008,chennai.... (2 Replies)
Discussion started by: gokulj
2 Replies

10. Shell Programming and Scripting

AWK pattern matching, first and last

In a nutshell, I need to work out how to return the last matching pattern from an awk //,// search. I can bring back the first, but am unsure how to obtain the last, and a simple tail won't work as the match could be over multiple lines. Secondly I would like some way of pattern matching, a... (10 Replies)
Discussion started by: smb_uk
10 Replies
Login or Register to Ask a Question