LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-23-2008, 08:04 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Rep: Reputation: 30
parsing text using sed/awk or similar???


Hi,

I have a text file (conf) like:

Code:
Section AAA
        var1 = abba
        var2 = baab
        var3 = abab
EndSection

Section BBB
        var1 = nisse
        var2 = lasse
        var3 = salle
EndSection
I want to get out sections individually into a variable (bash) but I can't get my head around sed how to do it. Then eventually get each variable correctly. Any ideas?

Thanks,
Indy
 
Old 07-23-2008, 11:27 AM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Each section of variables could be put into an array:-
Code:
#!/bin/bash
AAA=("" $(sed -n '/Section AAA/, /EndSection/ s/.* = \(.*\)/ \1/p' filename))
echo ${AAA[1]} ${AAA[2]} ${AAA[3]}

abba baab abab
Or all the variables could be put into one array:-
Code:
#!/bin/bash
all=("" $(sed -n 's/.* = \(.*\)/ \1/p' filename))
echo ${all[1]} ${all[2]} ${all[3]}
echo ${all[4]} ${all[5]} ${all[6]}

abba baab abab
nisse lasse salle
 
Old 07-23-2008, 11:46 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I read it as wanting each SECTION in a variable.

Section = `sed -n '/AAA/,/EndSection/p' filename`

I just realized that, unlike many regexes, the "range" construct in SED is not greedy--ie, it stops on the first instance of "EndSection"
 
Old 07-23-2008, 05:51 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
In perl (awk, too, but there are others in this forum who do better awk than do I), this is a one-liner:
Code:
perl -e '$/="\n\n"; @sections=<>; foreach $section (@sections){ print "\nSECTION = $section\n";
--- rod.

Last edited by theNbomr; 07-24-2008 at 09:02 AM.
 
Old 07-23-2008, 08:06 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
set -- $(awk -F"=" '/Section/{f=1;next}/EndSection/{f=0}f{print $2}' file)
echo $1
echo $2
 
Old 07-24-2008, 04:04 AM   #6
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
thanks lads,

Works beautifully

Indy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 5 07-26-2011 11:54 AM
Sed or Awk question, looking for parsing help rwartell Linux - Software 2 05-17-2006 11:59 PM
Sed or Awk question, looking for parsing help rwartell Programming 1 05-17-2006 04:42 PM
How do I cut out a specific piece of a html page (using sed/awk or similar)? bomix Linux - General 2 10-08-2005 04:30 PM


All times are GMT -5. The time now is 12:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration