LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2005, 05:06 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Need help with file parsing


I have a file that looks something like this:

blah blah
... lots of stuff ...
blah blah
FrameBegin 1
... more stuff ...
FrameEnd
.. more stuff ...

I need to get the "more stuff" (in bold) - it's several lines. If it's easier to have "FrameBegin" and "FrameEnd" included in the results, that's fine. Actually, there are 100 FrameBegin/FrameEnds that I will be pulling out into separate files, but if I know how to do one, I can figure out how to do them all. I have a feeling I can do it with something simple like sed and awk as opposed to writing a c++ program or perl script., but I'm having trouble.

Does anyone have an idea?

Thanks
 
Old 09-02-2005, 05:57 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This could help (awk based):

Code:
#!/bin/bash

awk '
  # only match lines between FrameBegin and FrameEnd (included)
  /^FrameBegin/,/^FrameEnd/ {
    # If first field equals FrameBegin, create outputfile ($1 + $2)
    if ( $1 ~ /^FrameBegin/ ) {
      outputfile = $1 $2
    }
    # if line does not start with Frame, put it in appropriate outputfile
    if ( $1 !~ /^Frame/ ) {
      print $0 >> outputfile
    }
}
' $1
Save and execute as: progname infile

I took the liberty to create outputfilenames based on FrameBegin 1, FrameBegin 2 etc. (the number being the unsure thing....). It's there to give you an idea and can be changed to your liking.

The importent part is the /^FrameBegin/,/^FrameEnd/ part. This makes sure that only the Frames and wat is inside are targeted.

You can do something simular with sed: sed -n '/^FrameBegin/,/^FrameEnd/p' infile.

Hope this clears things up a bit.

Last edited by druuna; 09-02-2005 at 05:59 PM.
 
Old 09-02-2005, 05:58 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally posted by druuna

The importent part is the /^FrameBegin/,/^FrameEnd/ part. This makes sure that only the Frames and wat is inside are targeted.

You can do something simular with sed: sed -n '/^FrameBegin/,/^FrameEnd/' infile.

Hope this clears things up a bit.
That was exactly what I was looking for - didn't know about the use of the comma in a regexp.

Thanks for the code too.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
config file parsing in C/C++ kadissie Programming 12 08-26-2009 05:19 PM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM
Mail file parsing paddyjoy Programming 4 08-07-2005 10:52 PM
Need help parsing text file scilec Programming 5 12-02-2004 01:00 PM
need help parsing text file airman99 Linux - General 2 10-08-2004 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration