LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2012, 10:33 AM   #1
jguion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
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.
Old 02-14-2012, 09:06 PM   #2
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware, Debian, Gentoo, FreeBSD, OpenBSD
Posts: 208

Rep: Reputation: 41
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.
 
Old 02-14-2012, 10:31 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 02-15-2012, 07:19 AM   #4
seb1525
LQ Newbie
 
Registered: Jan 2008
Distribution: RedHat
Posts: 5

Rep: Reputation: Disabled
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
 
2 members found this post helpful.
Old 02-15-2012, 03:11 PM   #5
jguion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
It works now!

Thanks everyone!

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'
 
Old 02-15-2012, 06:57 PM   #6
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware, Debian, Gentoo, FreeBSD, OpenBSD
Posts: 208

Rep: Reputation: 41
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.
 
1 members found this post helpful.
Old 02-16-2012, 08:28 AM   #7
jguion
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the tip

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK; print array in loop no output cristalp Programming 6 12-12-2011 02:14 PM
[SOLVED] Problem in dispaly of output using awk in array iteration. saurabhmehan Linux - Newbie 4 07-29-2010 11:04 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration