Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A little bit of sed/awk magic

Posted on 2011-09-07
5
Medium Priority
?
292 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 1000 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 1000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

618 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