Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 23rd, 2012, 03:40 PM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
Regarding awk start and end pattern

Hi,

In a file I am trying to find a pattern in this way.
File.txt

Line 1...
Line 2...
...
...
Start Pattern
...
...
XXXXEnd Pattern
pattern line 1...
pattern line 2...
YYYYEnd Pattern
...
...
...
ZZZZEnd Pattern
...
...

EOF

So using `awk '/Start Pattern/,/End Pattern/' File.txt` I can get the following:
XXXXEnd Pattern
pattern line 1...
pattern line 2...
YYYYEnd Pattern
Now I want the End pattern line only. Meaning, in this case YYYYYEnd Pattern and NOT the ZZZEnd Pattern line/s.

I tried `awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\n" '{print $4}' or `awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\r" '{print $4}'
But I receive nothing. The reason behind, the whole return string is not separated by LF or CR. I am really surprised.
Well, when I tried this
[color=blue]`awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\n" '{print $1}'
Output is:
XXXXEnd Pattern
pattern line 1...
pattern line 2...
YYYYEnd Pattern
Which means awk is returning the whole string without \n\r or what?
Need an explanation please.

Reply With Quote
  #2  
Old May 23rd, 2012, 05:23 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
Your examples given the file contents shown do not, to my tired eyes, make sense.
Your first awk command outputs 4 lines, that last one of which is the line you want? If so, just pipe that output into a tail -1?
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #3  
Old May 24th, 2012, 08:23 AM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
To process each occurance one by one...

Hi Simon,
Thanks for your reply.
That really helped.

But I forgot to mention one important thing here.

The start/end patterns are repetitive in the file.
So, I want to process this pattern occurrence one by one.

eg.
line1..
line2..
# First pattern
start pattern
...
...
end pattern
....
....
# Second pattern
start pattern
...
...
end pattern
...
...
# Third pattern
start pattern
...
...
end pattern
...
line n...



When I execute `awk '/start pattern/,/end pattern/' file.txt` I get
start pattern
...
...
end pattern
start pattern
...
...
end pattern
start pattern
...
...
end pattern

Now, how can I process each occurrence one by one?

Reply With Quote
  #4  
Old May 24th, 2012, 10:13 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
Ok, you're going to have to do this more 'progammatically' ...
Off the top of my head:

Code:
awk 'BEGIN { start=0; }
  /start pattern/ { start=1; }
  /end pattern/ { if (start == 1) { print; start=0; } }' 
  file.txt

Should work for you ... at awk start, set a flag variable to 0, then when you find a 'start pattern' set it to 1 to indicate it's been found.
Also search for 'end pattern' and if we have already found the start pattern this is the line we want, so print it, and set the flag variable back to 0.
Comments on this post
sujoydc agrees: Thank you very much !

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Regarding awk start and end pattern

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap