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

ya801
  • 69
  • 6

2 Answers2

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
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
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 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 – ya801 Jun 23 '20 at 15:20
  • Do you have these string e.g. `TEXT1, TEXT2, TEXT3, TEXT4` etc in some array or variable already? – anubhava Jun 23 '20 at 15:22
  • 1
    @ya801 sounds like you need to update your question to clarify what it is you want and provide more truly representative sample output. – Ed Morton Jun 23 '20 at 15:22
  • @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 – ya801 Jun 23 '20 at 15:26
  • @EdMorton, my bad i badly explained it for more generic use – ya801 Jun 23 '20 at 15:27
  • 1
    No problem, update your question to clarify what it is you want and provide more truly representative sample output. – Ed Morton Jun 23 '20 at 15:30