ksh : need to store the output of a awk command to a array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh : need to store the output of a awk command to a array
# 1  
Old 10-08-2013
Lightbulb ksh : need to store the output of a awk command to a array

I have awk command :

Code:
 awk -F ' ' '{ print $NF }' log filename

And it gives the output as below:

Code:
06:00:00
parameters:
SDS
(2)
no
no
no
no

doc=4000000000).


information:
0
5898
5898
06:06:25

The problem is i need to save this in a array.

For example, when print or echo $array[0]
i should get 06:00:00 and similarly

Code:
$array[1] = parameters:
.
.
.
$array[n] = 06:06:25

my end goal is to print them using a print statement i.e

Code:
printf("start time: %d  and end time: %d", array[0], array[n]")

Output:

Code:
start time:06:00:00 and end time:06:06:25

Kindly help...
# 2  
Old 10-08-2013
How about
Code:
array=( $(awk -F ' ' '{ print $NF }' log filename) )

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-08-2013
Lightbulb

Quote:
Originally Posted by RudiC
How about
Code:
array=( $(awk -F ' ' '{ print $NF }' log filename) )

I had tried this earlier but i was getting this error

Code:
ksh: 0403-057 Syntax error: `(' is not expected.

# 4  
Old 10-08-2013
A bit surprising. From this site's ksh(93) man pages:
Quote:
varname=(assign_list) No space is permitted between varname and the =. An assign_list can be one of the following: word ... Indexed array assignment.
What ksh version do you use?
# 5  
Old 10-08-2013
Lightbulb

Quote:
Originally Posted by RudiC
A bit surprising. From this site's ksh(93) man pages:

What ksh version do you use?
I use ksh88 version
# 6  
Old 10-10-2013
I can't help, then. Sorry.
# 7  
Old 10-10-2013
Code:
$ set -A arr parameters: 06:06:25
$ print ${arr[0]}
parameters:
$ print ${arr[1]}
06:06:25

So, if you want elements separated by white spaces:

Code:
set -f
set -A array $(awk '{ print $NF }' filename)
set +f

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Store value in array with awk

Hi everybody I wanna store some values that r in a .txt file in some arrays for example I have: 32782 28 32783 02 32784 01 32785 29 32786 25 32787 25 32788 00 32789 25 32790 02 32791 29 32792 23 32793 01 32794 28 and I need to save the first... (4 Replies)
Discussion started by: Behrouzx77
4 Replies

2. Shell Programming and Scripting

Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file. Empl No. Employee Age 1000000 22 1100000 24 1200000 26 Now, I want to pass an array having three different ages which need to replace the... (7 Replies)
Discussion started by: Nishi_Licious
7 Replies

3. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

4. 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

5. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

6. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

7. Shell Programming and Scripting

To store output of ls command

Hi, I need to pass a run time input to unix script which tracks the list of files in the directory under certain pattern. The files which are matched to the given pattern then need to be stored in the array. Requesint you all to please suggest me the solution. I gave the below command in the... (13 Replies)
Discussion started by: Sekar1
13 Replies

8. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

9. Shell Programming and Scripting

ksh: How to store each output line into a different variable?

Example output: /tmp/generatelines.sh line1 line2 line3 line4 I want each output line assigned to its own variable, ie: "line1" --> $a "line2" --> $b "line3" --> $c "line4" --> $d Is this possible without writing to a temporary file? Thanks (4 Replies)
Discussion started by: ksheller
4 Replies

10. Shell Programming and Scripting

Creating an Array in KSH from output (stdout)

Using Cygwin PDksh - But also have tested it on Linux with same results ---- I have a script that invokes a program/script and returns a string of data (1234 "12 34 56" 6789) and using "set -A" inserting it into an array. script code snipit >> get_array=$(php... (2 Replies)
Discussion started by: carlos25
2 Replies
Login or Register to Ask a Question