Sponsored Content
Top Forums Shell Programming and Scripting How to changes rows to columns in a file Post 302220182 by summer_cherry on Thursday 31st of July 2008 04:38:50 AM
Old 07-31-2008
hi,

You may use below perl version.

It can transpose any matrix.

Two parameter, 1>filename, 2>delimeter

Code:
package Utl;
BEGIN {
use Exporter();
@ISA = qw(Exporter);
@EXPORT = qw (&ChangeMetrix);
}

sub ChangeMetrix
{
        if ($#_<1){
                print "Usage: ChangeMetrix filename delimeter\n";
                exit;
        }
        $file=shift;
        $del=shift;
        open(FH,"<$file");
        while(<FH>){
                $_=~tr/\n//d;
                @arr=split($del,$_);
                $col=$#arr;
                for($i=0;$i<=$#arr;$i++){
                        $index=sprintf("%s%s",$.,$i);
                        $hash{$index}=$arr[$i];
                }
                $row=$.;

        }
        close(FH);
        for($i=0;$i<=$col;$i++){
                for($j=1;$j<=$row;$j++){
                        $temp=sprintf("%s%s",$j,$i);
                        print $hash{$temp},$del;
                }
                print "\n";
        }

}
return 1;

or you can use below easy one to meet your ques:
cat file | paste - -

Last edited by summer_cherry; 08-04-2008 at 12:31 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Columns to Rows in a File

Hi I have a input file in the format ABC,111,2008Q2, 49K ABC,111,2008Q3, 0K ABC,111,2008Q4, 0K ABC,222,2008Q2, 49K ABC,222,2008Q3, 0K ABC,222,2008Q4, 0K XYZ,111,2008Q2, 49K XYZ,111,2008Q3, 0K XYZ,111,2008Q4, 0K XYZ,222,2008Q2, 49K XYZ,222,2008Q3, 0K XYZ,222,2008Q4, 0K The output file... (3 Replies)
Discussion started by: chrismt
3 Replies

2. Shell Programming and Scripting

Rows to Columns - File Transpose

Hi I have an input file and I want to transpose it but I need to take care that if any field is missing for a record it should be popoulated with space for that field - using a shell script INFILE ---------- emp=1 sal=2 loc=abc emp=2 sal=21 sal=22 loc=xyz emp=5 loc=abc OUTFILE... (10 Replies)
Discussion started by: 46019
10 Replies

3. Shell Programming and Scripting

Changing file with rows and columns into just 1 column

Hi I need a bash shell script that will take text files with 4 rows and different numbers of columns in each row and convert each one into a text file with just one column. I then subtract 1.5 from each number in the column (I have that part already) The next step after that is I want to have... (10 Replies)
Discussion started by: ac130pilot
10 Replies

4. Shell Programming and Scripting

Large file - columns into rows etc

I have done a couple of searches on this and have found many threads but I don't think I've found one that is useful to me - probably because I have very basic comprehension of perl and beginners shell so trying to manipulate a script already posted maybe beyond my capabilities.... Anyway - I... (26 Replies)
Discussion started by: Myrona
26 Replies

5. UNIX for Dummies Questions & Answers

Audit Flat File - # of Columns / Rows

We receive a file which usually has 40 to 50 million rows. I want to set up an audit process by which everytime we receive a file we audit it for # of rows and total number of columns. So if the # of rows is around 1 million on a particular day, I want to raise a flag or send an email....and if... (3 Replies)
Discussion started by: priya33184
3 Replies

6. Shell Programming and Scripting

Convert columns to rows in a file

Hello, I have a huge tab delimited file with around 40,000 columns and 900 rows I want to convert columns to a row. INPUT file look like this. the first line is a headed of a file. ID marker1 marker2 marker3 marker4 b1 A G A C ... (5 Replies)
Discussion started by: ryan9011
5 Replies

7. UNIX for Dummies Questions & Answers

Swapping the columns of a text file for a subset of rows

Hi, I'd like to swap the columns 1 and 2 of a space-delimited text file but only for the first 1000 rows. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Converting rows to columns in csv file

Hi, I have a requirement to convert rows into columns. data looks like: c1,c2,c3,.. r1,r2,r3,.. p1,p2,p3,.. and so on.. output shud be like this: c1,r1,p1,.. c2,r2,p2,.. c3,r3,p3,.. Thanks in advance, (12 Replies)
Discussion started by: Divya1987
12 Replies

9. 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

10. Shell Programming and Scripting

Character screening and paste into new file in columns instead of rows

QL10169_SAUJANA%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KCRN11,MeContext=QL10169_SAUJANA_5 %External_Link_Failure %X2_link_problem_to_one_or_several_neighbouring_eNodeBs. QL10187_MATANG_JAYA_2%SubNetwork=ONRM_ROOT_MO_R,SubNetwork=ERBS_KUCHING,MeContext=QL10187_MATANG_JAY A_2_3... (2 Replies)
Discussion started by: Ankit Vyas
2 Replies
Exporter::Lite(3pm)					User Contributed Perl Documentation				       Exporter::Lite(3pm)

NAME
Exporter::Lite - Lightweight exporting of variables SYNOPSIS
package Foo; use Exporter::Lite; # Just like Exporter. @EXPORT = qw($This That); @EXPORT_OK = qw(@Left %Right); # Meanwhile, in another piece of code! package Bar; use Foo; # exports $This and &That. DESCRIPTION
This is an alternative to Exporter intended to provide a lightweight subset of its functionality. It supports "import()", @EXPORT and @EXPORT_OK and not a whole lot else. Unlike Exporter, it is not necessary to inherit from Exporter::Lite (ie. no "@ISA = qw(Exporter::Lite)" mantra). Exporter::Lite simply exports its import() function. This might be called a "mix-in". Setting up a module to export its variables and functions is simple: package My::Module; use Exporter::Lite; @EXPORT = qw($Foo bar); now when you "use My::Module", $Foo and "bar()" will show up. In order to make exporting optional, use @EXPORT_OK. package My::Module; use Exporter::Lite; @EXPORT_OK = qw($Foo bar); when My::Module is used, $Foo and "bar()" will not show up. You have to ask for them. "use My::Module qw($Foo bar)". Methods Export::Lite has one public method, import(), which is called automaticly when your modules is use()'d. In normal usage you don't have to worry about this at all. import Some::Module->import; Some::Module->import(@symbols); Works just like "Exporter::import()" excepting it only honors @Some::Module::EXPORT and @Some::Module::EXPORT_OK. The given @symbols are exported to the current package provided they are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. Otherwise an exception is thrown (ie. the program dies). If @symbols is not given, everything in @Some::Module::EXPORT is exported. DIAGNOSTICS
'"%s" is not exported by the %s module' Attempted to import a symbol which is not in @EXPORT or @EXPORT_OK. 'Can't export symbol: %s' Attempted to import a symbol of an unknown type (ie. the leading $@% salad wasn't recognized). BUGS and CAVEATS Its not yet clear if this is actually any lighter or faster than Exporter. I know its at least on par. OTOH, the docs are much clearer and not having to say "@ISA = qw(Exporter)" is kinda nice. AUTHORS
Michael G Schwern <schwern@pobox.com> LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html SEE ALSO
Exporter, Exporter::Simple, UNIVERSAL::exports perl v5.10.0 2006-11-11 Exporter::Lite(3pm)
All times are GMT -4. The time now is 03:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy