Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2010
Registered User
 

Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 03-14-2010
Registered User
 

Join Date: Mar 2009
Posts: 175
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
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
Sponsored Links
    #3  
Old 03-14-2010
alister alister is offline Forum Advisor  
Registered User
 

Join Date: Dec 2009
Posts: 1,496
Thanks: 39
Thanked 308 Times in 270 Posts
Hello umaars:

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

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

Demonstration:

Code:
$ 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
    #4  
Old 03-14-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts

Code:

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";

Sponsored Links
    #5  
Old 03-14-2010
thillai_selvan's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post
You can use the following code also

Code:
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] "
}


Last edited by thillai_selvan; 03-14-2010 at 11:48 PM..
Sponsored Links
    #6  
Old 03-14-2010
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 

Join Date: Jun 2006
Posts: 2,632
Thanks: 43
Thanked 383 Times in 373 Posts

Code:
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

Sponsored Links
    #7  
Old 03-14-2010
rekha_sri's Avatar
Registered User
 

Join Date: Feb 2010
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

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


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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rearrange columns and rows with awk Tom46 UNIX for Dummies Questions & Answers 3 07-17-2009 03:22 AM
# of rows and columns kylle345 Shell Programming and Scripting 3 07-02-2009 08:15 PM
Search for & edit rows & columns in data file and pipe tintin72 UNIX for Dummies Questions & Answers 8 03-27-2009 09:26 AM
convert rows into columns loperam Shell Programming and Scripting 2 12-01-2008 02:57 AM
Columns to rows mgirinath Shell Programming and Scripting 16 11-29-2007 06:03 PM



All times are GMT -4. The time now is 03:50 AM.