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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-23-2008, 08:04 AM
|
#1
|
|
Member
Registered: Nov 2002
Posts: 194
Rep:
|
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
|
|
|
|
07-23-2008, 11:27 AM
|
#2
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 323
Rep: 
|
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
|
|
|
|
07-23-2008, 11:46 AM
|
#3
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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"
|
|
|
|
07-23-2008, 05:51 PM
|
#4
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,257
|
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.
|
|
|
|
07-23-2008, 08:06 PM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
set -- $(awk -F"=" '/Section/{f=1;next}/EndSection/{f=0}f{print $2}' file)
echo $1
echo $2
|
|
|
|
07-24-2008, 04:04 AM
|
#6
|
|
Member
Registered: Nov 2002
Posts: 194
Original Poster
Rep:
|
thanks lads,
Works beautifully
Indy
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|