awk: need to extract a line before a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: need to extract a line before a pattern
# 8  
Old 08-02-2006
Quote:
Originally Posted by xinusys
hey man u r not so clearabout what u want?cleat itmate.then it would be easier to give u thesolution.
This is few lines of the big file :
....
32 Origine Tr Id : ******* : 0x0dfe5f0d
33
34 CMPT PORTION : Len = 0x1e
35 Component type : 0x0000a1 Invoke
36 Invoke Id : 0x000006 0x06
37 Local Operation: 0x00002d
38 ------------------ fin decodage TCAP ----------------------
39 ------------------ decodage MAP ----------------------------
40 OperationCode : 0x00002d Send Routing Info
41 MSISDN :
42 Extension : 1------- No Extension
43 Nat Of Add : -001---- INTERNATIONAL
44 Num Plan : ----0001 ISDN E164
45 MSISDN : ******** 93799322549
....

I want to get the "Origine Tr Id" line, here the line 32, (Origine Tr Id ...) for a specific MSISDN number (93799322549) (here at the line 45).

Thanks. npn35
# 9  
Old 08-02-2006
don't go by the 'line numbers' - go by the 'patterns':
one pattern being 'Origine Tr Id' - 'cache it' once found.
another pattern is ' MSISDN : ******** 93799322549' - once found output the 'cached' "Origine Tr Id" pattern.

give it a shot.

Last edited by vgersh99; 08-02-2006 at 01:47 PM..
# 10  
Old 08-03-2006
Try this:

Code:
  awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

I used line no + 13 as per your file

Quote:
32 Origine Tr Id : ******* : 0x0dfe5f0d
33
34 CMPT PORTION : Len = 0x1e
35 Component type : 0x0000a1 Invoke
36 Invoke Id : 0x000006 0x06
37 Local Operation: 0x00002d
38 ------------------ fin decodage TCAP ----------------------
39 ------------------ decodage MAP ----------------------------
40 OperationCode : 0x00002d Send Routing Info
41 MSISDN :
42 Extension : 1------- No Extension
43 Nat Of Add : -001---- INTERNATIONAL
44 Num Plan : ----0001 ISDN E164
45 MSISDN : ******** 93799322549

Last edited by vish_indian; 08-03-2006 at 03:20 AM.. Reason: Was printing incorrect line
# 11  
Old 08-03-2006
I created this file by duplicating your records

Quote:
32 Origine Tr Id : ******* : 0x0dfe5f0d
33
34 CMPT PORTION : Len = 0x1e
35 Component type : 0x0000a1 Invoke
36 Invoke Id : 0x000006 0x06
37 Local Operation: 0x00002d
38 ------------------ fin decodage TCAP ----------------------
39 ------------------ decodage MAP ----------------------------
40 OperationCode : 0x00002d Send Routing Info
41 MSISDN :
42 Extension : 1------- No Extension
43 Nat Of Add : -001---- INTERNATIONAL
44 Num Plan : ----0001 ISDN E164
45 MSISDN : ******** 93799322549
46 Origine Tr Id : ******* : 0x2efe5f0d
47
48 CMPT PORTION : Len = 0x1e
49 Component type : 0x0000a1 Invoke
50 Invoke Id : 0x000006 0x06
51 Local Operation: 0x00002d
52 ------------------ fin decodage TCAP ----------------------
53 ------------------ decodage MAP ----------------------------
54 OperationCode : 0x00002d Send Routing Info
55 MSISDN :
56 Extension : 1------- No Extension
57 Nat Of Add : -001---- INTERNATIONAL
58 Num Plan : ----0001 ISDN E164
59 MSISDN : ******** 93799327649
Running the command,
Code:
awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

gives the output
32 Origine Tr Id : ******* : 0x0dfe5f0d
46 Origine Tr Id : ******* : 0x2efe5f0d
# 12  
Old 08-03-2006
Quote:
Originally Posted by vish_indian
Try this:

Code:
 awk '$0~/Origine Tr Id/{lineno=NR} NR==lineno+13 && $0~/MSISDN/{print}' tfile

I used line no + 13 as per your file



Code:
 awk '$0~/Origine Tr Id/{lineno=NR} NR==lineno+13{print}' tfile

could also be used
Thank you . It's a very nice solution .
Regards
# 13  
Old 08-03-2006
Dear npn35,

Please use the updated code

Old code: awk '$0~/Origine Tr Id/{lineno=NR} NR==lineno+13 && $0~/MSISDN/{print}' tfile
would print the line containing MSISDN number.

Since you need the Origine Tr Id
use:

Code:
awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

# 14  
Old 08-03-2006
Quote:
Originally Posted by vish_indian
Dear npn35,

Please use the updated code

Old code: awk '$0~/Origine Tr Id/{lineno=NR} NR==lineno+13 && $0~/MSISDN/{print}' tfile
would print the line containing MSISDN number.

Since you need the Origine Tr Id
use:

Code:
awk '$0~/Origine Tr Id/{lineno=NR; text=$0} NR==lineno+13 && $0~/MSISDN/{print text}' tfile

Dear vish_indian,

Thanks again . This is exactly what I need .
I will adapt the command if I need others informations from the data file .

BR , npn35
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 extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 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

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

[Awk] Extract block of with a particular pattern

Hi, I have some CVS log files, which are divided into blocks. Each block has many fields of information and I want to extract those blocks with a pattern. Here is the sample input. RCS file: /cvsroot/eclipse/org.eclipse.debug.core/core/org/eclipse/debug/core/DebugPlugin.java,v head: 1.174... (7 Replies)
Discussion started by: sandeepk1611
7 Replies

5. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

6. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

7. Shell Programming and Scripting

Extract pattern from text line

The text line has the following formats: what.ever.bla.bla.C01G06.BLA.BLA2 what.ever.bla.bla.C11G33.BLA.BLA2 what.ever.bla.bla.01x03.BLA.BLA2 what.ever.bla.bla.03x05.BLA.BLA2 what.ever.bla.bla.Part01.BLA.BLA2 and other similar ones, I need a way to select the "what.ever.bla.bla" part out... (4 Replies)
Discussion started by: TehOne
4 Replies

8. Shell Programming and Scripting

Extract pattern from text line

Hi, the text line looks like this: "test1" " " "test2" "test3" "test4" "10" "test 10 12" "00:05:58" "filename.bin" "3.3MB" "/dir/name" "18459" what's the best way to select any of it? So I can for example get only the time or size and so on. I was trying awk -F""" '{print $N}' but... (3 Replies)
Discussion started by: TehOne
3 Replies

9. Shell Programming and Scripting

Extract pattern from text line

Gents, from these sample lines: ZUCR.MI ZUCCHI SPA RISP NC 2,5000 6 ott 0,0000 ZV.MI ZIGNAGO VETRO 3,6475 16:36 Up 0,0075 is it possible to get this: ZUCR.MI 2,5000 ZV.MI 3,6475 i.e. the first field, a separator and the first decimal number? (in Europe we... (9 Replies)
Discussion started by: vampirodolce
9 Replies

10. Shell Programming and Scripting

extract a particular start and end pattern from a line

hi In the foll example the whole text in a single line.... i want to extract text from IPTel to RTCPBase.h. want to use this acrooss the whole file Updated: IPTel\platform\core\include\RTCPBase.h \main\MWS2051_Sablime_Int\1... (7 Replies)
Discussion started by: manish205
7 Replies
Login or Register to Ask a Question