Data in Rows to Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Data in Rows to Columns
# 1  
Old 03-14-2010
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.
# 2  
Old 03-14-2010
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
# 3  
Old 03-15-2010
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-15-2010
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";

# 5  
Old 03-15-2010
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-15-2010 at 01:48 AM..
# 6  
Old 03-15-2010
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

# 7  
Old 03-15-2010
Bug

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting data from specific rows and columns from multiple csv files

I have a series of csv files in the following format eg file1 Experiment Name,XYZ_07/28/15, Specimen Name,Specimen_001, Tube Name, Control, Record Date,7/28/2015 14:50, $OP,XYZYZ, GUID,abc, Population,#Events,%Parent All Events,10500, P1,10071,95.9 Early Apoptosis,1113,11.1 Late... (6 Replies)
Discussion started by: pawannoel
6 Replies

2. Shell Programming and Scripting

Transpose comma delimited data in rows to columns

Hello, I have a bilingual database with the following structure a,b,c=d,e,f The right half is in a Left to right script and the second is in a Right to left script as the examples below show What I need is to separate out the database such that the first word on the left hand matches the first... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Transpose Data from Columns to rows

Hello. very new to shell scripting and would like to know if anyone could help me. I have data thats being pulled into a txt file and currently have to manually transpose the data which is taking a long time to do. here is what the data looks like. Server1 -- Date -- Other -- value... (7 Replies)
Discussion started by: Mikes88
7 Replies

6. Shell Programming and Scripting

Help for a Perl newcomer! Transposing data from columns to rows

I have to create a Perl script which will transpose the data output from my experiment, from columns to rows, in order for me to analyse the data. I am a complete Perl novice so any help would be greatly appreciated. The data as it stands looks like this: Subject Condition Fp1 ... (12 Replies)
Discussion started by: Sarah_W
12 Replies

7. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

8. UNIX for Dummies Questions & Answers

Convert Rows to Columns Specific Data

I have this data M36 AREA INFORMATION MDN = 0485009346 ESN = H'15fda0b0 TYPE = HLR RESULT = NOK REASON = UNRECOGNIZED MIN COMPLETED AREA INFORMATION MDN = 0498044402 ESN = H'15fdac11 TYPE... (2 Replies)
Discussion started by: krabu
2 Replies

9. Shell Programming and Scripting

Transpose columns to Rows : Big data

Hi, I did read a few posts on the subjects, tried out a few solutions, but did not solve my problem. https://www.unix.com/302121568-post11.html https://www.unix.com/shell-programming-scripting/137953-large-file-columns-into-rows-etc-4.html Please help. Problem very similar to the second link... (15 Replies)
Discussion started by: genehunter
15 Replies

10. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies
Login or Register to Ask a Question