Solved

A little bit of sed/awk magic

Posted on 2011-09-07
5
281 Views
Last Modified: 2012-05-12
Hi guys,

I'm looking for an expert who is familiar with sed and awk, I need to parse a text file using Bash. My text file looks something like this:

; this is a comment

[mapping]

; some more comments here
foo=bar
apple    =   banana

; and more comments
       qwerty=dvorak

[statement]
this is a block of text which should
be read verbatim, without any
; parsing of any kind = that was applied
to the = mapping section 

above.

Open in new window


Basically what I need to do is:

1) In the "[mapping]" section of the file:
    a) Ignore lines that are pure whitespace or that start with a semicolon
    b) Get each mapping pair, e.g. "foo" and "bar", "apple" and "banana" etc
    c) Trim whitespace off the pairs

2) In the "[statement]" section of the file
    a) Get the entire block of text following the line that says [statement], verbatim, without any of the parsing or formatting that was done in the [mapping] section.

I hope this isn't too complicated. It doesn't need to be really robust, the example I gave above is about as bad as the text file will get and it can die horribly if it is malformed. I'm not concerned much with efficiency either (the file I'm parsing is always going to be pretty small), so whatever solution can read the file in over and over again, re-parse things more than it needs to etc.
0
Comment
Question by:Frosty555
  • 3
5 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 36498730
1)

awk '/\[mapping\]/,/\[statement\]/ {if($0~"=") print $1 $2 $3}' inputfile

2) Where does the [statement] section end?
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36499522
A2.
awk 'BEGIN{found=0;} /\[statement\]/ {found=1;} {if(found==1) print}'  /path-to-inputfile | tail -n+2
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36499769
Here is another version of A2 by perl command line

perl -ne 'if (/\[statement\]/){$state=1; next;} print if ($state eq 1);'  /path-to-inputfile
0
 
LVL 38

Assisted Solution

by:wesly_chen
wesly_chen earned 250 total points
ID: 36499782
Small modification of awk in #36499522
awk 'BEGIN{found=0;} /\[statement\]/ {found=1;next} {if(found==1) print}'  /path-to-inputfile
0
 
LVL 31

Author Comment

by:Frosty555
ID: 36543381
woolmilkporc:
awk '/\[mapping\]/,/\[statement\]/ {if($0~"=") print $1 $2 $3}' inputfile

Open in new window

This successfully parses out the contents of the [mapping] section.

For your question, the [statement] block ends at the end of the file.

wesly_chen:
awk 'BEGIN{found=0;} /\[statement\]/ {found=1;next} {if(found==1) print}'  /path-to-inputfile

Open in new window

This successfully grabs the [statement] block.

Thanks guys!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now