awk - To retrieve an expression from the last line containing a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - To retrieve an expression from the last line containing a pattern
# 1  
Old 08-17-2014
awk - To retrieve an expression from the last line containing a pattern

Hi All,

I'm new on this forum, and i'm trying since several days to find out a way to retrieve a expression from the last line containing a pattern. Could you please help me with this ?

E.g. The file is containing the following lines


Code:
08/05 17:33:47  STAT1 Response(22) is '1,00664060,A3204,3P3,P'
08/05 17:33:47  STAT4 WriteMessage: wrote message   1,00664060,A3204,3P3,P                                                                                                        
08/05 17:34:18  STAT1 Response(22) is '1,00458008,A3316,3P3,P'
08/05 17:34:18  STAT4 WriteMessage: wrote message   1,00458008,A3316,3P3,P                                                                                                             
08/05 17:34:28  STAT1 Response(22) is '1,01881038,A3204,3P3,P'
08/05 17:34:28  STAT4 WriteMessage: wrote message   1,01881038,A3204,3P3,P                                                                                                             
08/05 17:34:35  STAT1 Response(22) is '1,00458008,A3318,3P3,P'
08/05 17:34:35  STAT4 WriteMessage: wrote message   1,00458008,A3318,3P3,P                                                                                                             
08/05 17:34:43  STAT1 Response(22) is '1,00527072,A3316,3P3,P'
08/05 17:34:43  STAT4 WriteMessage: wrote message   1,00527072,A3316,2P3,P                                                                                                             
08/05 17:35:00  STAT1 Response(22) is '1,00527072,A3318,3P3,P'
08/05 17:35:00  STAT4 WriteMessage: wrote message   1,00527072,A3318,3P3,P                                                                                                             
08/05 17:36:07  STAT1 Response(22) is '1,01294070,A3204,3P3,P'
08/05 17:36:07  STAT4 WriteMessage: wrote message   1,01294070,A3204,3P3,P                                                                                                             
08/05 17:37:05  STAT1 Response(22) is '1,00458008,A1104,3P3,P'
08/05 17:37:05  STAT4 WriteMessage: wrote message   1,00458008,A1104,5P3,P                                                                                                             
08/05 17:37:18  STAT1 Response(22) is '1,00458008,A1106,3P3,P'
08/05 17:37:18  STAT4 WriteMessage: wrote message   1,00458008,A1106,3P3,P                                                                                                             
08/05 17:37:24  STAT1 Response(22) is '1,00527072,A1104,3P3,P'
08/05 17:37:24  STAT4 WriteMessage: wrote message   1,00527072,A1104,3P3,P                                                                                                             
08/05 17:37:35  STAT1 Response(22) is '1,00527072,A1106,3P3,P'
08/05 17:37:35  STAT4 WriteMessage: wrote message   1,00527072,A1106,4P3,P                                                                                                             
08/05 17:38:37  STAT1 Response(22) is '1,00859040,A3204,3P3,P'
08/05 17:38:37  STAT4 WriteMessage: wrote message   1,00859040,A3204,3P3,P                                                                                                             
08/05 17:40:44  STAT1 Response(22) is '1,00533016,A3202,3P3,P'
08/05 17:40:44  STAT4 WriteMessage: wrote message   1,00533016,A3202,3P3,P                                                                                                             
08/05 17:40:48  STAT1 Response(22) is '1,02597032,A3318,3P3,P'
08/05 17:40:48  STAT4 WriteMessage: wrote message   1,02597032,A3318,3P3,P                                                                                                             
08/05 17:40:53  STAT1 Response(22) is '1,00533016,A3204,3P3,P'
08/05 17:40:53  STAT4 WriteMessage: wrote message   1,00533016,A3204,3P3,P                                                                                                             
08/05 17:42:31  STAT1 Response(22) is '1,01514018,A3204,3P3,P'
08/05 17:42:31  STAT4 WriteMessage: wrote message   1,01514018,A3204,1P3,P                                                                                                             
08/05 17:43:19  STAT1 Response(22) is '1,02597032,A1104,3P3,P'
08/05 17:43:19  STAT4 WriteMessage: wrote message   1,02597032,A1104,3P3,P                                                                                                             
08/05 17:43:30  STAT1 Response(22) is '1,02597032,A1106,3P3,P'
08/05 17:43:30  STAT4 WriteMessage: wrote message   1,02597032,A1106,3P3,P

If the pattern is 02597032, awk should return me '3P3'

If the pattern is 00527072, awk should return me '4P3'

I have to recognize that i'm struggling a little bit, making the difference beween the separators and the columns ! Smilie

In Any case, thanks a lot in advance for your support.

Last edited by bartus11; 08-17-2014 at 10:32 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 08-17-2014
Please use code tags as required by forum rules!

Try
Code:
awk -F, 'match ($0,pattern) {T=$4} END {print T}' pattern="00527072" file
4P3

# 3  
Old 08-17-2014
Thanks RudyC for your reply on this

Please find below what i'm getting keying your command :
Code:
[val@REDHAT-1 tmp]$ awk -F, 'match ($0,pattern) {T=$4} END {print T}' pattern="00527072" test
3P3

Same Result by including the pattern value directly between the brackets :
Code:
[val@REDHAT-1 tmp]$ awk -F, 'match ($0,"00527072") {T=$4} END {print T}' test3P3

It looks like awk is not returning the latest expression matching with the pattern.
# 4  
Old 08-17-2014
Are you using exactly the same sample file given above? As you can see, it gave 4P3 when I tested...
# 5  
Old 08-17-2014
RudyC forgot my last post !!!

There was additional lines in the bottom of my file that i missed !!!!!Smilie

Thanks a lot ! It works like a charm and i can now continue to work on my script.

---------- Post updated at 04:49 PM ---------- Previous update was at 04:44 PM ----------

Thanks Again for your assistance.

I've amended the title with the [SOLVED] comment
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to format each line by pattern

The four lines in the tab-delimeted input are a sample format from my actual data. The awk is meant to go line by line and check if a pattern is satisfied and if it is follow a particular format (there are 3). All the lines in the file should follow one of the three formats below. I added comments... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

4. Shell Programming and Scripting

PERL: retrieve the data based on regular expression

Hi Friends i have a code below sample $text contains the values test1 PIC X test1 PIC XX test1 PIC XXX test1 PIC X(8) test1 PIC X(12) test1 PIC X test1 X(8) test1 PIC X VALUE 'N'. $text =~ /^\d{6} +(\d{2}) +(+) +PIC +(+)(\((\d+)\)(V(+)| +(COMP\-3).|\.)|( +(COMP\-3).|... (4 Replies)
Discussion started by: i150371485
4 Replies

5. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

6. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

7. Shell Programming and Scripting

awk + pattern search with regular expression

Hi , I have a file with "|" (pipe) as a delimeter. I am looking for the record count where 5th field is a number with 15 digit length only. all the records with above requirement is valid rest all are invalid. I need count of valid records and invalid records. Can anyone please help (9 Replies)
Discussion started by: vikash_k
9 Replies

8. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies

9. UNIX for Dummies Questions & Answers

retrieve lines that match a pattern

Hi, I would like to know how can I get lines from a text file that match no more than 2 '>'. Example: Input file: a >cr1 4 a>b b>c a >cr2 5 a>b Output file: a >cr2 5 a>b Thanks in advance (2 Replies)
Discussion started by: fadista
2 Replies

10. Shell Programming and Scripting

how can i conjunct two expression in one line in awk?

hi im trying to conjuct two expresssion , but not getting the exact result i need.. i want to conjuct 1) awk < /etc/passwd 'NR >15' and 2)awk < /etc/passwd '!/8niaz/' how will i execute , so it will start printing from the 16th line and will omit the lines which include '8niaz'. ... (1 Reply)
Discussion started by: niyas_gk
1 Replies
Login or Register to Ask a Question