LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-01-2015, 05:28 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684

Another quick alternative with awk:
Code:
awk '{for(i=1;i<=NF;i++)a[i] = a[i] (NR > 1?" ":"") $i}END{for(j in a)print a[j]}' file
This just shifts the first loop to be as file is read.

Ruby alternative:
Code:
ruby -ane 'a||=[];a << $F;END{ a.transpose.each{|x| puts x.join(" ")} }' file
 
1 members found this post helpful.
Old 08-01-2015, 05:55 AM   #17
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Nice one grail, the concern I have with printing like that is that assoc arrays don't guarantee order, so I used a similar printing loop to astrogeek.
Maybe PROCINFO["sorted_in"] might be an option if everyone is at the required level - last (RHEL 6) commercial site I worked in wasn't.
 
1 members found this post helpful.
Old 08-01-2015, 03:29 PM   #18
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,876
Blog Entries: 1

Rep: Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997Reputation: 1997
Quote:
Originally Posted by grail View Post
Another quick alternative with awk:
Code:
awk '{for(i=1;i<=NF;i++)a[i] = a[i] (NR > 1?" ":"") $i}END{for(j in a)print a[j]}' file
This just shifts the first loop to be as file is read.
Very nice further minimalization!

Works for all my own test cases on my Slackware 14.1 machine.

Quote:
Originally Posted by syg00 View Post
Nice one grail, the concern I have with printing like that is that assoc arrays don't guarantee order, so I used a similar printing loop to astrogeek.
Maybe PROCINFO["sorted_in"] might be an option if everyone is at the required level - last (RHEL 6) commercial site I worked in wasn't.
This is kind of uncharted territory for me as I am not an awk guru of any kind. But I run FreeBSD so I thought it would provide at least a test of an alternate awk. This is BWK awk version 20121220 (FreeBSD).

First the test file and Slackware 14.1 results:

Code:
# cat alpha.txt
a b c
de fg hi
jkl mno pqr

# awk -f corner.awk alpha.txt
a de jkl
b fg mno
c hi pqr

# awk -f grail.awk alpha.txt
a de jkl
b fg mno
c hi pqr
So far so good, now FreeBSD awk:

Code:
# awk -f corner.awk alpha.txt
a de jkl
b fg mno
c hi pqr

# awk -f grail.awk alpha.txt
b fg mno
c hi pqr
a de jkl
So it turns out that the array ordering does not hold across awk versions as syg00 pointed out!

I tried a BEGIN block defining PROCINFO["sorted_in"] = "@ind_num_asc" without success. It produces no error but I really do not know if this awk recognizes it at all.

It also occurs to me that input ordering is actually not the problem, rather it is non-deterministic ordering of the output, again as syg00 pointed out. So I am not sure "sorted_in" is the correct parameter and am unfamiliar with these options in any event.

However, making the print loop deterministic fixes it:

Code:
# cat grail.awk
{
        for(i=1;i<=NF;i++)
                a[i] = a[i] (NR > 1?" ":"") $i
}
END{
        #for(j in a)print a[j]
        for(j=1;j<=NF;++j)print a[j]
}

# awk -f grail.awk alpha.txt
a de jkl
b fg mno
c hi pqr

Last edited by astrogeek; 08-01-2015 at 03:43 PM. Reason: Added comment about PROCINFO
 
Old 08-01-2015, 10:39 PM   #19
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
That PROCINFO is a gawk extension - reasonably recent.
There used to be a detailed change history of gawk with specific version the change came in, but I can't find it nowadays.

"sorted_in" is not just for input - you are indicating you want to process an array as if it was "sorted in that order".
 
1 members found this post helpful.
Old 08-02-2015, 05:04 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
This link also confirms what you found with regard to BWK and others: http://www.gnu.org/software/gawk/man...nning-an-Array

And here is detail on PROCINFO and sorting: http://www.gnu.org/software/gawk/man...lling-Scanning
 
1 members found this post helpful.
  


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] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 11:31 AM
[SOLVED] Perl script for manipulating rows and columns captainentropy Programming 10 08-26-2013 03:45 AM
Script to convert words in a text file [columns to rows] naveenchandar Linux - Newbie 2 11-08-2012 02:00 AM
[SOLVED] transform rows into columns in bash xeon123 Linux - Newbie 4 07-30-2011 08:19 AM
Script to convert logs columns to rows fono Linux - Software 10 05-19-2009 09:29 PM


All times are GMT -5. The time now is 02:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration