Solved

A little bit of sed/awk magic

Posted on 2011-09-07
5
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to moveā€¦

726 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