LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Transforming array into a string with awk, not getting right output (http://www.linuxquestions.org/questions/programming-9/transforming-array-into-a-string-with-awk-not-getting-right-output-929305/)

jguion 02-14-2012 10:33 AM

Transforming array into a string with awk, not getting right output
 
I need to sort a csv list in output from lsmod, but am having trouble getting the right output.

This works, showing that the array has been properly sorted:
jguion:~$ echo x_tables 14004 6 ipt_ULOG,xt_limit,xt_tcpudp,xt_NFQUEUE,ipt_set,ip_tables|awk '{split($4,arr,",");n=asort(arr);for(i=1;i<=n;i++)print arr[i];print $1, $2, $3};'
ip_tables
ipt_ULOG
ipt_set
xt_NFQUEUE
xt_limit
xt_tcpudp
x_tables 14004 6
jguion:~$

But, when I add in the code from a standard awk 'join' function (I cannot use awk libraries for this), the final output is not sorted:
jguion:~$ echo x_tables 14004 6 ipt_ULOG,xt_limit,xt_tcpudp,xt_NFQUEUE,ipt_set,ip_tables|awk '{split($4,arr,",");n=asort(arr);sep=",";for(i=1;i<=n;i++)out = out (i > n ? sep : "") arr[i];print $1, $2, $3, $out};'
x_tables 14004 6 x_tables 14004 6 ipt_ULOG,xt_limit,xt_tcpudp,xt_NFQUEUE,ipt_set,ip_tables
jguion:~$

If anyone can point out my error or suggest a better way of doing this, it would be terrific.

padeen 02-14-2012 09:06 PM

It's hard to decode without proper formatting. Next time put the code between [code]...[/code] tags.

Is the first example correct? After the for loop, you don't use {...} so what is happening is the first statement only ('print arr[i]') is executing, then at the end the final 'print $1, $2, $3'. Is that what you want?

If you put {...} around the two print statements you get different output
Code:

ip_tables
x_tables 14004 6
ipt_ULOG
x_tables 14004 6
ipt_set
x_tables 14004 6
xt_NFQUEUE
x_tables 14004 6
xt_limit
x_tables 14004 6
xt_tcpudp
x_tables 14004 6

which is obviously wrong.

In a nutshell, I don't understand what the 'print $1,$2,$3' statement is there for, so I don't understand what you are trying to achieve with it.

grail 02-14-2012 10:31 PM

Please show a before and after of what the data should look like? I am unable to follow your example to see what your issues is :(

seb1525 02-15-2012 07:19 AM

The error is that in the second command you wrote:

print $1, $2, $3, $out

which should be:

print $1, $2, $3, out

What happened is that $out tries to take the "out"th argument; since the value of "out" is a string, it's interpreted as zero, so it's the equivalent of writing

print $1, $2, $3, $0

In other words, the entire input line, not the sorted result, got printed.

Changing $out to out produced the correct output.

- seb

jguion 02-15-2012 03:11 PM

It works now!
 
Thanks everyone!:newbie:

The answer from seb1525 was the right one, it works great, now!

As to the other two responders:
1 - there was no formatting because the code is a one-liner. I know it makes it look ugly, but that is the way it will in the application that runs the command.

2 - The before and desired after output are right there. See the 'echo x_tables 14004 6 ipt_ULOG,xt_limit,xt_tcpudp,xt_NFQUEUE,ipt_set,ip_tables' part of the command for the 'before'

And see the output line shown near the bottom: 'x_tables 14004 6 x_tables 14004 6 ipt_ULOG,xt_limit,xt_tcpudp,xt_NFQUEUE,ipt_set,ip_tables'

padeen 02-15-2012 06:57 PM

Glad you got it sorted. Re the formatting, it's helpful to put it in code tags even if it is a one-liner because then it will display in mono font.

jguion 02-16-2012 08:28 AM

Thanks for the tip
 
Thanks to Padeen for the tip on the code tags, I will make sure to use them in the future! :)


All times are GMT -5. The time now is 06:05 AM.