LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-26-2011, 11:54 AM   #1
Rizla
Member
 
Registered: Aug 2003
Location: Bristol, Uk
Distribution: Fedora Core 4
Posts: 48

Rep: Reputation: 15
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
 
Old 01-26-2011, 12:42 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Rizla View Post
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}")
 
Old 01-26-2011, 02:54 PM   #3
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
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.
 
Old 01-26-2011, 03:10 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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}
 
Old 01-26-2011, 06:53 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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
 
Old 01-26-2011, 10:47 PM   #6
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
Hi grail,

oops!
corrected, thx.
 
  


Reply



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
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 2 12-17-2009 09:10 AM
Help me in creating my first shell (bash) script. mq15 Linux - Newbie 2 07-30-2009 11:11 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-01-2008 11:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration