Storing command output in an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing command output in an array
# 1  
Old 05-09-2013
Storing command output in an array

Hi,
I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below-
Code:
for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq`

want to store-
Code:
my_array[] = `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq`

and then want to do-
Code:
for str in my_array[]
do
...
done

shell is korn shell, sorry for syntax mistakes.

thanks in advance.Smilie

Last edited by Scott; 05-09-2013 at 11:02 AM.. Reason: Please use code tags
# 2  
Old 05-09-2013
Code:
my_array=($(awk -F: '$3 >100 {print $1}' /etc/passwd | uniq))
echo ${my_array[1]}
syslog

Code:
for str in ${my_array[*]}
do
echo "$str"
done

PS: No need to cat data to awk, when it can read it self.
Back tics '' is old way of wrapping code, its better to use parentheses $()

Edit:
you can also remove the uniqe
Code:
my_array=($(awk -F: '$3 >100 {!x[$1]++} END {for (i in x) print i}' /etc/passwd))


Last edited by Jotne; 05-09-2013 at 11:21 AM..
# 3  
Old 05-10-2013
Hi Jotne,
thanks for the input. by the way if i wan to append another array to this array my_array, how can I do that in korn shell?
like - my_array
then adding another array to it-
my_array += other_array;
as a result the my array should contain both the array elements after addition.

thanks,
# 4  
Old 05-10-2013
I do not now korn, but this works in bash
Code:
my_array=(${my_array[*]} ${other_array[*]})

# 5  
Old 05-12-2013
Hi Jotne,
thanks for the reply. The below will work for korn shell for appending one array to another array. The second array can be one already formed array with values or it can be list of values to be appended to first array (in OR part)
Code:
#my_array=(${my_array[*]} ${other_array[*]}) //in bash shell
set -A my_array ${my_array[*]} ${other_arr[*]} //ksh
OR
set -A my_arr "values/command_output"
set +A my_arr "other_values/another_command_output" #this appends other command output values to my_arr

hope, this helps.
thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

4. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

5. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

6. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

7. Shell Programming and Scripting

How to store output of command to an array

Hello Guys, I am trying to store output of command to an array and then want to print, but it showing an error that "bad substitution". I am not getting why it's happening. Can anyone help me? #!/usr/bin/sh hr=0 min=0 i=1 last $1 | grep -w `date "+%b"` | grep -v '\(0:.*\)' | grep -vw sshd... (8 Replies)
Discussion started by: kasparov
8 Replies

8. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

9. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

10. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question