[SOLVED] Transforming array into a string with awk, not getting right output
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Click here to see the post LQ members have rated as the most helpful post in this thread.
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
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.
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'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.