The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
urgent-extracting block data from flat file using shell script shirish_cd Shell Programming and Scripting 4 02-06-2008 09:05 AM
Extracting Data from a File oop UNIX for Dummies Questions & Answers 0 07-31-2007 12:48 PM
Extracting Data from xml file nishana Shell Programming and Scripting 3 07-13-2007 08:17 AM
Extracting data from an AFP file Dolph UNIX for Advanced & Expert Users 4 05-22-2007 05:29 AM
Parsing the data in a file Omkumar Shell Programming and Scripting 2 05-20-2005 09:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-07-2005
arminder arminder is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 5
Parsing file and extracting the useful data block

Greetings All!!

I have a very peculiar problem where I have to parse a big text file and extract useful data out of it with starting and ending block pattern matching.

e.g. I have a input file like this:

sample data
block1
sample data
start
useful data
end
sample data
block2
sample data
start
useful data
end
sample data

My output shall be like following:

block1
useful data
block2
useful data

Any help will be appreciable.

Cheers

Arminder
  #2 (permalink)  
Old 03-07-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Assuming that your input file is always of the same format as specified in your example, the following code will work. Save this as block.awk

Code:
#!/usr/bin/awk -f

/^block/ {
  print $0
  next
}
/^start/ {
  flag = 1
  next
}
flag == 1 {
  print $0
  flag = 0
}

Make it executable, run it over your input file, and redirect output if desired...

Code:
$ chmod +x ./block.awk
$ ./block.awk my_file > my_output
$ more my_output
block1
useful data
block2
useful data

Cheers
ZB
  #3 (permalink)  
Old 03-07-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
USe this,


Code:
 awk '{ if ($0 ~ "block") { print $0;getline;print $0 } }' testfile

hth.
  #4 (permalink)  
Old 03-07-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by muthukumar
USe this,


Code:
 awk '{ if ($0 ~ "block") { print $0;getline;print $0 } }' testfile

hth.
No, that won't work. The OP wants "useful data" not "sample data". You'd need three "getline"s to get this to work....

Code:
awk '{ if ($0 ~ "block") { print $0;getline;getline;getline;print $0 } }' testfile

Cheers
ZB
  #5 (permalink)  
Old 03-07-2005
muthukumar muthukumar is offline
Registered User
  
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
oops.. ZB you are 100% correct.
  #6 (permalink)  
Old 03-07-2005
arminder arminder is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 5
Sorry Guys, maybe I didn't put it correctly. The amount of "useful data" isn't known i.e. it may be 2 lines long or 10/20/100 i.e. no same occurences. Same goes for the "sample data" too. Its only that "start" and "end" is that can tell me where to start from and where to end.

Later I figured a way to print the block and so currently I am using following in awk file:

/^start/./^end/

Thereafter I grep out (grep -v) the start and end text.

Cheers

Arminder
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0