Use of AWK as array

Dear Experts,

I have a file containing text like below

123
456
789
012
345

I want to save each line in array.
and print each array index as per my requirement.

For Example array[2] will print 456
array[4] will print 012

Thanks for your Help.

awk '{ array[NR] = $0 } END { print array[2]; print array[4]; }' file

Dear era/friends,

Many thanks for your help!!
I have i more query.

I have a file with three columns and below mentioned script print all three columns perfectly.

awk '{bts[NR]=$1;tre[NR]=$2;bss[NR]=$3}
END{{printf "%s\t", bts[1]; printf "%s\t", tre[1];printf "%s\n", bss[1];} }'

But if i replace print command with my desired command it give me following error message:

./tre_alarm.sh: line 34: syntax error near unexpected token `('

Below is the script i used:
awk '{bts[NR]=$1;tre[NR]=$2;bss[NR]=$3}
END{for (i=1;i<=NR;i++){ omcdo -cmd 'TRE_reset(bts[i],bss[i],"tre[i]")'} }' Output_TRE_ALARM.txt

it is working fine when used on command prompt with out script:
Actual command:

omcdo -cmd 'TRE_reset(12,23,"27")'

Thanks for your help

Can anybody help me with this i will be very thankful to him.

actually when awk came across first ' it consider it as end so try \' for your command..

BOSS!!!
The new script is

awk '{bts[NR]=$1;tre[NR]=$2;bss[NR]=$3}
END{for (i=1;i<=NR;i++){ omcdo -cmd \'TRE_reset(bts[i],bss[i],"tre[i]")\'} }' Output_TRE_ALARM.txt

But still it is not working, what do you say about the inverted commas, should it be like this !!!

Thanks for the help!!

TRE_reset(bts[i],bss[i],"tre[i]")
what is this?? a function??

In awk programs you cannot execute commands directly like in shell.
You must use the system function (or the getline command if you want to retreive the output of the command).

awk -v Q="'" '{bts[NR]=$1;tre[NR]=$2;bss[NR]=$3}
END{for (i=1;i<=NR;i++){ system("omcdo -cmd" Q "TRE_reset(" bts "," bss ",\"" tre "\")" Q) } }' Output_TRE_ALARM.txt

In fact, you don't need to use arrays. You can do :

awk -v Q="'" '{ system("omcdo -cmd" Q "TRE_reset(" $1 "," $3 ",\"" $2 "\")" Q) ' Output_TRE_ALARM.txt

Jean-Pierre.

Actually it seems like the first unknown is already the "omcdo". It isn't a built-in awk function name; if it's an external program, you need to invoke it with the system() function or something. Or you could simply use awk to print out the command line(s) you want executed, and pipe the output to sh

Dear Friends,

Thank you very much for your comments.

But still i have some error:

To reset a TRE i have to use below mentioned command, under command prompt:which is a build in function for me.

omcdo -cmd 'TRE_reset(21,6,"24")'

I have to insert these three values from a file contains hundreds of values:
For example file : Output.txt

21 6 24
4 5 31
22 4 7
12 5 21
16 8 7
19 6 23

I need a script that extract values in array from file Output.txt and insert it in TRE build in function like above:

Please help me in this regard

Thanks a lot.:(:(:frowning:

You can do something like that (without array) :

while read V1 V2 V3
do
   omcdo -cmd \'TRE_reset($V1,$V2,"$V3")\'
done < Output.txt

Jean-Pierre.

BOSS!!! Still not working
Below is the test script

#/usr/sbin/ksh
set -x

while read V1 V2 V3
do
omcdo -cmd \'TRE_reset($V1,$V2,"$V3")\'
done < Output.txt

Its gives error message as given below:
./test.sh: line 6: syntax error near unexpected token `('
./test.sh: line 6: ` omcdo -cmd \'TRE_reset($V1,$V2,"$V3")\''

Please Reply
Thanks a lot in advance

Try :

#!/usr/sbin/ksh
set -x

while read V1 V2 V3
do
omcdo -cmd "TRE_reset($V1,$V2,\"$V3\")"
done < Output.txt

Jean-Pierre.

Dear aigles,

Still i have same error:

To reset a TRE i have to use below mentioned command, under command prompt:which is a build in function for me.
actual command:
omcdo -cmd 'TRE_reset(21,6,"24")'

I have to insert these three values from a file contains hundreds of values:
For example file : Output.txt

21 6 24
4 5 31
22 4 7
12 5 21
16 8 7
19 6 23

I need a script that extract values in array from file Output.txt and insert it in TRE build in function like above:

Please help me in this regard
Thanks a lot.

You say you "still have the same error" but the latest script by Aigles should specifically have fixed that. Did you try it exactly as it was posted here? Does omcdo require the arguments to be quoted in some way or does it suffice if it gets the text TRE_reset(21,6,"24") ... and are the double quotes around the last argument really required? Anyhow, the latest script should accomplish that, so either you didn't copy it correctly, or there is still some requirement which isn't clear.