1

I've the following awk command:

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

And it gives the output as below:

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 an array. For example when I print or echo $array[0]

I should get 06:00:00

and similarly

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

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

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

Output

start time:06:00:00 and end time:06:06:25
  • 1
    awk has arrays, why do you need to use ksh arrays? – Stéphane Chazelas Oct 8 '13 at 16:40
  • The printf statement as given will print 'start time: 6 and end time: 6', as the %d forces a conversion to integer. – Henk Langeveld Oct 9 '13 at 7:38
2

This should do it:

array=( $(awk -F ' ' '{ print $NF }' log filename) )

Given @Stephane's comment, another approach:

array=()
awk -F ' ' '{ print $NF }' log filename | while IFS= read -r line; do
    array+=( "$line" )
done
echo ${#array[@]}
echo ${array[1]}
echo ${array[17]}
17
06:00:00
06:06:25

works on my ksh "93u+"

  • 1
    That skips the empty lines and you'd have issues if the output contains tab or wildcard characters. – Stéphane Chazelas Oct 8 '13 at 16:38
  • You'd also have issues using bash, pdksh, or some other shells which put variables defined/modified on the RHS of a pipe operator into a subshell, as array would be empty after the loop. – dannysauer Oct 8 '13 at 18:26
  • True, but fortunately the tag is ksh. – glenn jackman Oct 8 '13 at 18:34
  • Many Linux systems and some other systems use pdksh as their "ksh". I'd put the awk command in the background as a ksh coprocess and then have read use the -p argument to read from that coprocess, personally. That works with both. – dannysauer Oct 8 '13 at 20:40
  • @glennjackman: I had tried the first approch earlier I was kicked saying this error. ksh: 0403-057 Syntax error: (' is not expected.` By tring your second approach I was getting the same error. ksh: 0403-057 Syntax error: (' is not expected.` – Ram Oct 9 '13 at 9:20
0

It looks like you just need the value of the last field of the first and last record.

This is best done in awk itself:

nawk 'NR == 1 {start=$NF}
    {last=$NF}
    END { 
         printf("start time: %s  and end time: %s", start, last)
    }' log filename
start time: 06:00:00  and end time: 06:06:25

Use an array indexed by NR (record number/line number) in awk if you want to access the fields at random:

nawk '{
    last[NR]=$(NF)
} END {
    print last[1],last[NR-1]
}' log
06:00:00 5898
  • @HenkLangevelt:What if I wanted get other values such as 0 and 5898 (last but one value) because if i want to make this one generic and change only the varibles to print differnt variable as per req. – Ram Oct 9 '13 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.