How to changes rows to columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to changes rows to columns in a file
# 1  
Old 07-31-2008
How to changes rows to columns in a file

Hi,

I have a small requirement in chainging the rows to columns. The below example.txt contains info as shown
Name:Person1
Age:30
Name:Person2
Age:40
Name:Person3
Age:50

I want to make it displayed as hown below
Name:Person1 Age:30
NameSmilieerson2 Age:40
Name:Person3 Age:50

I tried by doing the 'tr' command by chaning all new lines to carriage -return it dowsnt work.

i.e. cat example.txt|egrep -i 'Name|Age'|tr '\n' '\t\r' -- it doesnt serve me bcoz the \t is considered but not \r............Please some one help me
Smilie Smilie
# 2  
Old 07-31-2008
Code:
awk '{a=$0;getline;print a, $0}' file

# 3  
Old 07-31-2008
Thank You So much
# 4  
Old 07-31-2008
Hi

how should i have to use incase i like to use printf statement for formmating the text displayed ........


some thing like...........


printf "%-20s,%-10s" a, $0 ...could u pls help me
# 5  
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question