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. |
|
 |
01-26-2011, 11:54 AM
|
#1
|
|
Member
Registered: Aug 2003
Location: Bristol, Uk
Distribution: Fedora Core 4
Posts: 46
Rep:
|
Creating array from command output (bash shell script)
Hi, I have a command that outputs n lines of text, and I want to place each line into an array element, but I can't seem to get the syntax correct
so my command is this:
cat $configfile | sed -n '/cluster:'$clustername'/,/cluster/ p' | awk /host/
which produces many lines depending on the value of $clustername. I'd like to get each line as elements of an array,
any help would be much appreciated,
Thanks
|
|
|
|
01-26-2011, 12:42 PM
|
#2
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Quote:
Originally Posted by Rizla
Hi, I have a command that outputs n lines of text, and I want to place each line into an array element, but I can't seem to get the syntax correct
so my command is this:
cat $configfile | sed -n '/cluster:'$clustername'/,/cluster/ p' | awk /host/
which produces many lines depending on the value of $clustername. I'd like to get each line as elements of an array,
any help would be much appreciated,
Thanks
|
Hi,
you also need to fix your sed statement and the excessive piping.
Code:
#!/bin/bash
declare -a ARRAY
count=0
while read -r line; do
var[((count++))]="$line"
done < <(sed -n "/cluster:$clustername/,/cluster/ {/host/ p}" "${configfile}")
|
|
|
|
01-26-2011, 02:54 PM
|
#3
|
|
LQ Newbie
Registered: Apr 2010
Posts: 18
Rep:
|
Code:
#!/bin/bash
configfile=/path/to/file
clustername=whatever
while read -r line
do
[[ $line =~ cluster ]] && flag=0
[[ $line =~ cluster:${clustername} ]] && flag=1
if ((flag)) && [[ $line =~ host ]]; then Array+=("$line"); fi
done < "${configfile}"
printf '%s\n' "$Array[@]}"
I prefer not use external commands when I can avoid them.
without a representative sample of $configfile, it's hard to do better.
perhaps it's posible to do it without regexes.
Last edited by everToulouse; 01-26-2011 at 10:47 PM.
|
|
|
|
01-26-2011, 03:10 PM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
Another way is by setting the IFS variable to newline only (and restore it to the default later). Following the suggestion by crts:
Code:
OLD_IFS=${IFS}
IFS=$'\n'
ARRAY=( $(sed -n "/cluster:$clustername/,/cluster/ {/host/ p}" "${configfile}") )
IFS=${OLD_IFS}
|
|
|
|
01-26-2011, 06:53 PM
|
#5
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,310
|
@everToulouse - your Array is a string and not an Array. Try changing @ for a 1 and you will see there is no value stored there. Easy fix though, throw in some
brackets and your all good:
Code:
if ((flag)) && [[ $line =~ host ]]; then Array+=("$line"); fi
|
|
|
|
01-26-2011, 10:47 PM
|
#6
|
|
LQ Newbie
Registered: Apr 2010
Posts: 18
Rep:
|
Hi grail,
oops!
corrected, thx.
|
|
|
|
| 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 08:40 PM.
|
|
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
|
|