0

am trying to pretty print following existing solution

https://stackoverflow.com/questions/17692771/awk-sort-multidimensional-array/17706399#17706399

awk 'BEGIN {
    a[1][1] = "UP-H"
    a[1][2] = "RRR8"
    a[1][3] = "85554"
    a[1][4] = "H55"
    a[2][1] = "MM"
    a[2][2] = "454"
    a[2][3] = "X222"
    a[2][4] = "X77a"

    for (i=1; i in a; i++)
        for (j=1; j in a[i]; j++)
            printf "a[%d][%d] = %s\n",i,j,a[i][j]}'
        

will print

a[1][1] = UP-H
a[1][2] = RRR8
a[1][3] = 85554
a[1][4] = H55
a[2][1] = MM
a[2][2] = 454
a[2][3] = X222
a[2][4] = X77a

...
a[3][0] = asdf ...
....

but how loop over that array in order to pretty print that array along with generated/added text as below format

TEXT1 UP-H TEXT2 RRR8 TEXT3 85554 TEXT4 H55
TEXT1 MM   TEXT2 454  TEXT3 X222  TEXT4 X77a

i dont know what is best way if i have let say e.g. array "a" with multiple values UP-H,RRR8,85554,H55

0

2 Answers 2

1

Your question isn't clear but this may be what you're looking for:

$ cat tst.awk
BEGIN {
    a[1][1] = "UP-H"
    a[1][2] = "RRR8"
    a[1][3] = "85554"
    a[1][4] = "H55"
    a[2][1] = "MM"
    a[2][2] = "454"
    a[2][3] = "X222"
    a[2][4] = "X77a"

    OFS = "\t"
    split("TEXT1 ANOTHERTXT THREE ASDF",strs)
    for (i=1; i in a; i++) {
        for (j=1; j in a[i]; j++) {
            printf "%s%s %s", (j>1 ? OFS : ""), strs[j], a[i][j]
        }
        print ""
    }
}

.

$ awk -f tst.awk
TEXT1 UP-H  ANOTHERTXT RRR8 THREE 85554 ASDF H55
TEXT1 MM    ANOTHERTXT 454  THREE X222  ASDF X77a
Sign up to request clarification or add additional context in comments.

Comments

0

You may use this awk script:

cat prnt.awk

BEGIN {
    a[1][1] = "UP-H"
    a[1][2] = "RRR8"
    a[1][3] = "85554"
    a[1][4] = "H55"
    a[2][1] = "MM"
    a[2][2] = "454"
    a[2][3] = "X222"
    a[2][4] = "X77a"

   for (i=1; i in a; i++) {
       len = length(a[i])
       for (j=1; j <= len; j++)
           printf "TEXT1%d %s%s", j, a[i][j], (j<len?"\t":"\n")
   }
}

Run it as:

awk -f prnt.awk
TEXT11 UP-H TEXT12 RRR8 TEXT13 85554  TEXT14 H55
TEXT11 MM   TEXT12 454  TEXT13 X222   TEXT14 X77a

6 Comments

hello TEXT1 TEXT2 TEXT3 TEXT4 i meant for ilustration purpose is it possible to adjust that TEXT1 will remain , TEXT2 in fact is e.g. AAA, TEXT2 is BBBB, etc.. or i need to then after as mentioned by EdMorton that need tu run multiple sub for each TEXT1..4 to put there another customized text string except those
Do you have these string e.g. TEXT1, TEXT2, TEXT3, TEXT4 etc in some array or variable already?
@ya801 sounds like you need to update your question to clarify what it is you want and provide more truly representative sample output.
@anubhava TEXT1, TEXT2, TEXT3, TEXT4 is just any text to be added between , like TEXT1,ANOTHERTXT,THREE,ASDF in fact i meant that like this way
No problem, update your question to clarify what it is you want and provide more truly representative sample output.
|

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.