0

My two input files are pipe separated.

File 1 :

a|b|c|d|1|44

File 2 :

44|ab|cd|1

I want to store all my values of first file in array.

awk -F\| 'FNR==NR {a[$6]=$0;next}'
  1. So if I store the above way is it possible to interpret array; say I want to know $3 of File 1. How can I get tat from a[].

  2. Also will I be able to access array values if I come out of that awk?

Thanks

1
  • wrt 2 - when you exit a C program (or whatever your programming language of choice is) do you expect to be able to access the contents of the arrays and other variables you used inside that program? Awk is no different. Commented Feb 24, 2015 at 12:48

2 Answers 2

1

I'll answer the question as it is stated, but I have to wonder whether it is complete. You state that you have a second input file, but it doesn't play a role in your actual question.

1) It would probably be most sensible to store the fields individually, as in

awk -F \| '{ for(i = 1; i < NF; ++i) a[$NF,i] = $i } END { print a[44,3] }' filename

See here for details on multidimensional arrays in awk. You could also use the split function:

awk -F \| '{ a[$NF] = $0 } END { split(a[44], fields); print fields[3] }'

but I don't see the sense in it here.

2) No. At most you can print the data in a way that the surrounding shell understands and use command substitution to build a shell array from it, but POSIX shell doesn't know arrays at all, and bash only knows one-dimensional arrays. If you require that sort of functionality, you should probably use a more powerful scripting language such as Perl or Python.

If, any I'm wildly guessing here, you want to use the array built from the first file while processing the second, you don't have to quit awk for this. A common pattern is

awk -F \| 'FNR == NR { for(i = 1; i < NF; ++i) { a[$NF,i] = $i }; next } { code for the second file here }' file1 file2

Here FNR == NR is a condition that is only true when the first file is processed (the number of the record in the current file is the same as the number of the record overall; this is only true in the first file).

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Wintermute My i/p file is like 20150118140133|6282147664321|76761|20110421|19700101 20150118140133|6282157668986|62281|20141225| 20150118140133|6282157669102|3278294|20140618|20140906 I used all the three below awks. I am getting spaces :[ottstusr@osdinfbsdtapp1 mvann]$ awk -F \| '{ for(i = 1 ; i <= NF ; ++i) a[$NF,$i] = $i} END { print a[1,3] }' SPP_IN [ottstusr@osdinfbsdtapp1 mvann]$ awk -F \| '{ for(i = 1 ; i <= NF ; ++i) a[NF,i] = $i} END { print a[1,3] }' SPP_IN [ottstusr@osdinfbsdtapp1 mvann]$ awk -F \| '{ for(i = 1 ; i <= NF ; ++i) a[NF,i] = $i} END { print a[1,3] }' SPP_IN
0

To keep it simple, you can reach your goal of storing (and accessing) values in array without using awk:

arr=($(cat yourFilename |tr "|" " ")) #store in array named arr

# accessing individual elements
echo ${arr[0]}  
echo ${arr[4]}

# ...or accesing all elements
for n in ${arr[*]}
do
    echo "$n"
done

...even though I wonder if that's what you are looking for. Inital question is not really clear.

4 Comments

if my ip file is a|b|c|d w|r|g|h i want to store the value in array like a[1,1] = a a[1,2] = b a[2,1] = w Kindly suggest in any way to achive this. @Wintermute I ve two i/p files and need to do field level validation.
Bash doesn't support multidimensional arrays. In case you are interested in managing those two files in bash-script, you can try a fast-but-not-really-elegant solution like: arr1=($(cat yourFirstFilename |tr "|" " ")) arr2=($(cat yourSecondFilename |tr "|" " ")) ...but please, clarify what's your goal: Just managing the information in any way?? (I would suggest you to use bash for simple operations, perl or python for more demanding ones). Or you want/need to do it using awk?
Thanks for the reply. My goal is to do file comparsion. SAY first field of first record is null, second input file should have UNKNOWN in its first field of second record.
In this case I strongly recommend you to switch to Perl. [Here][1] you have a similar example you can easily adapt to your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.