Data in Rows to Columns

Hi,

I am a beginner in bash&perl.
I have data in form of:-

A 1
B 2
C 3
D 4
E 5

I would like your help to find a simple way to change it to :-

A B C D E
1 2 3 4 5

Any help would be highly appreciated.

cat abc.txt | perl -e '$i=0 ;
                            while(<>){
                             chomp;
                             ($row[$i],$col[$i])= split(" "); 
                             $i++;
                             } 
                             map {print "$_ " } @row; print"\n"; 
                             map {print "$_ " } @col;print "\n";'

HTH,
PL

Hello umaars:

Welcome to the forums. The following one way to accomplish that task using sh scripting:

while read a b; do
    x=$x\ $a
    y=$y\ $b
done < data
echo $x
echo $y

Demonstration:

$ cat data
A 1
B 2
C 3
D 4
E 5

$ unset x y

$ while read a b; do x=$x\ $a; y=$y\ $b; done < data; echo $x; echo $y
A B C D E
1 2 3 4 5

Cheers,
Alister

use strict;
use warnings;
my ($str,$str1);
open FH, "<file" or die "Can't Open $!";
while(<FH>)
{
    if(/([A-Z])\s+([0-9])/)
    {
        $str=$str." $1";
        $str1=$str1." $2";
    }
}
print "$str\n$str1\n";

You can use the following code also

open FH,"<file" or die "Can't open file : $!\n";
my @res;
while ( <FH> )
{
    push(@res,split);

}
for ( my $i=0; $i <= $#res; $i=$i+2 )
{
    print "$res[$i] "
}
print "\n\n";
for ( my $i=1; $i <= $#res; $i=$i+2 )
{
    print "$res[$i] "
}
awk '
{
for (i=1;i<=NF;i++)
{
 arr[NR,i]=$i;
 if(big <= NF)
  big=NF;
 }
}

END {
  for(i=1;i<=big;i++)
   {
    for(j=1;j<=NR;j++)
    {
     printf("%s%s",arr[j,i], (j==NR ? "" : OFS));
    }
    print "";
   }
}' urfile

Using bash script you can get the required output in simple way.

field1="$field1 `cut -d' ' -f 1 file`"
field2="$field2 `cut -d' ' -f 2 file`"
echo $field1
echo $field2

Thanks People.

Much appreciated!!

---------- Post updated at 11:36 AM ---------- Previous update was at 11:16 AM ----------

For anyone else who may want to use the below script to achieve what I desired.

It works when I change the second line to :-

field2="$field2 `cut -d' ' -f 3 file`"

You can whittle that down further still:

echo $(cut -d' ' -f1 data)
echo $(cut -d' ' -f2 data)

Cheers,
Alister