LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 11: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, 10:06 PM   #2
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian 7, FreeBSD, OpenBSD
Posts: 179

Rep: Reputation: 35
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, 11:31 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,648

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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, 08: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, 04: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, 07:57 PM   #6
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian 7, FreeBSD, OpenBSD
Posts: 179

Rep: Reputation: 35
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, 09: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:14 PM
[SOLVED] Problem in dispaly of output using awk in array iteration. saurabhmehan Linux - Newbie 4 07-29-2010 12:04 PM
Awk output to bash array? kj6loh Programming 4 09-07-2009 01: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 01:49 PM


All times are GMT -5. The time now is 08:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration