I'm still learning Unix and I'm having issues understanding the following line of code.
echo "$lines" | awk '{split($0,a,":"); print a[3],a[2],a[1]}'
I don't understand what is happening with the array a in the line of code above. Is it declaring the array and setting it equal to the string it's parsing? If it is declaring the array a, then, why can't I print out the results later on in the code?
echo "${a[1]}"
The line above prints an empty line and not what has been stored in the array a when the string was parsed. I know there is always something in the string that needs to be parsed and when I call the array a[1] I know that I'm in inside the scope. I just don't see/understand what is happening with the array a that prevents me from printing it out later on in the code.
awkarrayais only available in theawkscript; it is not a part of the shell. That's why you can't access it in theechocommand later.a=( $(echo "$lines" | awk '{ … }') )to do a shell array assignment. However, that would process all lines into a single shell array. You'd have to decide whether that's OK. It would also lose any spaces in the array elements (so if one of the lines containedaa bb:cc dd:ee ff gg, you'd get 7 shell array elements from the 3 Awk array elements). You'll have to judge whether that's a problem for you.