move output of awk to array

Hi experts,

I have a the following awk command,

awk '{print $1}' /users/jon/list4.txt. The output is

123
787
888
...
...

I want to move the output to array using shell programming.

My shell is tcsh.

Is it possible to move to array using shell porg? I know its possible in perl.

Let me know

Thanks

Amit

with this data would be populated in the array

awk '{ arr[$1] }' filename 
# array=$(awk '{print $1}' /users/jon/list4.txt)
# for items in $array; do echo $items; done

Hi madhan,
Thanks much for the help.
One doubt, How do I print the array? if I use this

awk '{ arr[$1] }' filename

Thank
Amit

I am getting the following error,
when I executed,

# array=$(awk '{print $1}' /users/jon/list4.txt)
# for items in $array; do echo $items; done

syntax error at line 21: `array=$' unexpected

array=`awk '{print $1}' /users/jon/list4.txt`

Hi,

Thanks again, one more doubt,
How do I access the value in array(6).

Thx
amit

depending on the shell you are using

# set -- $array 
# echo $6

or else , if you just want to get the 6th field, you can do it inside awk. If you list4.txt is just one column, your 6th item would be NR==6

# result=`awk 'NR==6{print}' list4.txt`
# echo "this is my 6th item : $result"

Hi,

My shell is tcsh,

What I am looking for is,

array=`awk '{print $1}' /users/jon/list4.txt`
array2=`awk '{print $1}' /users/jon/list5.txt`

If value in array[] Not equal array2[]
then
echo value of arra[] >> ~/noval.txt

can I do this?

Thanks

Amit

# awk '{print $1}' /users/jon/list4.txt > 1.txt
# awk '{print $1}' /users/jon/list5.txt > 2.txt
# diff 1.txt 2.txt
# echo $? #use this to check whether the 2 files are different. 

I leave the rest of the code to you. before you diff the 2 files, use sort to sort the items , or you can sort them by piping the output of awk to sort.

While I agree with the method ghostdog74 proposes, the user specifically asked for a tcsh solution. Here is one that should work provided the files are small.

#!/bin/tcsh

set a=`awk '{print $1}' file1`
echo $a
echo "$a[2]"
echo "$#a"

set b=`awk '{print $1}' file2`
echo $b
echo "$b[2]"
echo "$#b"

if ($#a == $#b) then
   echo "number of lines match"
else
   echo "number of lines do not match"
endif

if ("$a" == "$b") then
   echo "files match"
else
   echo "files do not match"
endif

Hi,

when we do line by line compare for file1 and file2 , how do I extract the value in file1 which did not match that in file2.

Will it work even if sort, because there could be some order in file2.

Let me know

Thanks

Amit
Amit

Hi murphy,

how do I get the line which did not match? from your script

Pls help

Amit

first, sort the 2 files. man sort for more information.
then, use comm to compare them. man comm for more information

Thanks ..it worked with comm..

Happy Xmas & New Year folks

Amit