Solved

A little bit of sed/awk magic

Posted on 2011-09-07
5
285 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fedora linux on laptop - setup sendmail - or some kind of email 5 66
AWK:  END { statements } 2 28
sed/awk/tail: how to read 3'de last line 4 44
Linux 3 38
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

730 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