Kev's decision considers reverse text in every word. You, for example, the conclusion, do not show this, but its key point is the use of the function.
You have the code you need, you just need to rebuild it a bit.
cat file1 aa bb cc foo do as cat commandFile function reverse( line ) { n=split(line, tmpLine) for (j=n; j>0; j--) { printf("%s ",tmpLine[j] ) } }
Launch
awk -f commandFile file1
Exit
as do foo cc bb aa
There were a few minor changes that I made using n=split(line, tmpLine) ... print tmpLine[j] is a common method for parsing an input line in a function to print it. I don't think vars $ 1 has a region from the value passed from the array (your value [i]), so I changed it to split..tmpLine [j]. I also found that the variable "i" from the END section was stored in scope in the reverse function, so I changed it to j to fix this.
I had to figure out a few things, so the debug version that I used is presented below.
If you have access to gawk, it will be useful for you to learn how to use the debugger that is available. If you use awk / gawk / nawk on systems without a debugger, then this is one way to understand what is going on in your code. If you redirect your programs to a file or channel, And if the system supports the notation "/ dev / stderr", you can print debug lines there, i.e.
#dbg print "#dbg0:line=" line > "/dev/stderr"
Some systems have other designations for accessing stderr, so if you do so much, you should find out what is available.
cat commandFile.debug function reverse( line ) { n=split(line, tmpLine) #dbg print "#dbg0:line=" line #dbg print "#dbg1:n=" n "\tj=" j "\ttmpLine[j]=" tmpLine[j] for (j=n; j>0; j--) { #dbg print "#dbg2:n=" n "\tj=" j "\ttempLine[j]=" tmpLine[j] printf("%s ",tmpLine[j] ) } } # main loop { a[NR]=$0 } # print reversed array #dbg END{ print "AT END"; for(i=NR; i>0; i--) printf( "#dbg4:i=%d\t%s\n%s\n", i, a[i] , reverse(a[i]) ) } END{ for(i=NR; i>0; i--) printf( "%s\n", reverse(a[i]) ) }
Hope this helps.