1

My input 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 achieve this in awk bash.

I have two i/p files and need to do field level validation.

2
  • I know it's not quite what you asked, but when you start doing bash/awk arrays, I start thinking it's time to break out more fully featured scripting languages. Is there a reason you might not want to perl or python it? Commented Feb 25, 2015 at 13:23
  • @Sobrique the time to move to perl or python is when you want to do more than just manipulate text, otherwise standard UNIX tools are perfectly capable of doing the job concisely, efficiently, and portably. Commented Feb 25, 2015 at 21:57

3 Answers 3

1

Like this

awk -F'|' '{for(i=1;i<=NF;i++)a[NR,i]=$i} 
           END {print a[1,1],a[2,2]}' file

Output

a r

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

Comments

1

This parses the file into an awk array:

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

You'll have to add code that uses the array for this to be of any use, of course. Since you didn't say what you wanted to do with the array once it is complete (after the pass over the file), this is all the answer i can give you.

Comments

1

You're REALLY going to want to get/use gawk 4.* if you're using multi-dimensional arrays as that's the only awk that supports them. When you write:

a[1,2]

in any awk you are actually creating a psedudo-multi-dimensional array which is a 1-dimensional array indexed by the string formed by the concatenation of

1 SUBSEP 2

where SUBSEP is a control char that's unlikely to appear in your input.

In GNU awk 4.* you can do:

a[1][2]

(note the different syntax) and that populates an actual multi-dimentional array.

Try this to see the difference:

$ cat tst.awk
BEGIN {
    SUBSEP=":"  # just to make it visible when printing

    oneD[1,2] = "a"
    oneD[1,3] = "b"

    twoD[1][2] = "c"
    twoD[1][3] = "d"

    for (idx in oneD) {
        print "oneD", idx, oneD[idx]
    }
    print ""

    for (idx1 in twoD) {
        print "twoD", idx1
        for (idx2 in twoD[idx1]) {      # you CANNOT do this with oneD
            print "twoD", idx1, idx2, twoD[idx1][idx2]
        }
    }
}
$ awk -f tst.awk
oneD 1:2 a
oneD 1:3 b

twoD 1
twoD 1 2 c
twoD 1 3 d

Comments

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.