LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-29-2011, 06:47 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
transform rows into columns in bash


Hi,

I would like to transform rows into columns from a file in bash.

This is the file
Code:
param1         value1
param2         value2
param3         value3
And I like to become like this:

Code:
param1   param2    param3
value1   value2    value3
How can i do this?
 
Old 07-29-2011, 08:23 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by xeon123 View Post
Hi,

I would like to transform rows into columns from a file in bash.

This is the file
Code:
param1         value1
param2         value2
param3         value3
And I like to become like this:

Code:
param1   param2    param3
value1   value2    value3
How can i do this?

Something like this?
Code:
{
  for (i=1;i<=NF;i++)
  {
    arr[NR,i]=$i;
    if(nf<= NF)
      nf=NF;
  }
  nr=NR
}

END {
  for(i=1;i<=nf;i++)
  {
    for(j=1;j<=nr;j++)
    {
      printf("%s\t",arr[j,i]);
    }
    printf("\n");
  }
}


Cheers,
Tink
 
Old 07-29-2011, 11:14 PM   #3
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
The problem with bash is that it does not support multi-dimensional arrays - so you have to use awk, perl, pthyon, ruby or suchlike to transpose the contents of your file.

Here is one way of doing what you want to do using Python and zip*
Code:
!/usr/bin/python

inputList = []

infile = open('infile')
for lines in infile:
    fields = lines.split(None, 1)
    fieldsTuple = (fields[0], fields[1].rstrip())
    inputList.append(fieldsTuple)

infile.close()

inputList = zip(*inputList)

outfile = open("outfile", "w")
for items in inputList:
    for item in items:
        outfile.write(str(item) + ' ')
    outfile.write('\n')

outfile.close()
Infile:
Code:
"param1"  "value1"
"param2"  "value2"
"param3"  "value3"
Outfile:
Code:
"param1" "param2" "param3" 
"value1" "value2" "value3"

Last edited by fpmurphy; 07-29-2011 at 11:16 PM.
 
Old 07-30-2011, 01:12 AM   #4
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slackware 12.2, scientific linux 6.4, knoppix 7.2, salix 14.1
Posts: 871

Rep: Reputation: 80
Code:
for i in 1 2 ; do cat input | cut -d' ' -f$i | paste -s >> output ; done
it works for me but it is ugly solution.
 
Old 07-30-2011, 07:19 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Well as with the others my solution only works for the 2 column scenario as well:
Code:
awk '{print $1;s[NR]=$2}END{printf "\n";for(i in s)print s[i]}' ORS=" " file
 
  


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
Formatting rows and columns kdelover Programming 4 08-31-2010 02:56 AM
a2ps - How to get 80 rows by 130 columns packer_fan Linux - Software 1 08-28-2010 11:06 AM
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
columns & rows Ammad Linux - General 1 08-08-2005 04:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 06:47 PM


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

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