switch line to colomn


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting switch line to colomn
# 1  
Old 09-11-2008
switch line to colomn

Hi
It's possible to switch the line to colon from a file using Perl or AWK?
for example my file have somthing like this:
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34

I want to have a file with the line switched like :
10 20 30
11 21 31
12 22 32
13 23 33
14 24 34

Thanks a lot
# 2  
Old 09-11-2008
A perl hint

Throwing some perl up here...


This will put your text into an array of arrays - look at it for a bit and think about how you'd pull elements from this array into a new AoA
- Hint:
get the first element from the each row(array) in original AoA
Place them into the first row (first row) of the new AoA
Repeat with the second element from each row(array) in the original
Place them into the second row(array) of the new AoA

Code:
open(FILE, "<$ARGV[0]") or die "Cannot open $ARGV[0] for read :$!";
@list = <FILE>;
close( FILE );

$AoA[$i] = [ @list ];
for $aref ( @AoA ) {
   print "[\n @$aref ],\n";
}

# 3  
Old 09-11-2008
nawk -f invertMatirx.awk myFile.txt

invertMatrix.awk:
Code:
{
  for(i=1; i<=NF; i++)
    a[i, NR]=$i
  nf=NF;nr=NR
}
END {
  for(f=1; f<=nf;f++)
     for(r=1; r<=nr;r++)
       printf("%s%s", a[f,r], (r==nr) ? "\n" : FS)
}

# 4  
Old 09-12-2008
thanks!
# 5  
Old 09-12-2008
Code:
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,$_);
                if($#arr>$col){
                        $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);
                        if(exists $hash{$temp}){
                                print $hash{$temp},$del;
                        }
                        else{
                                print "**",$del;
                        }
                }
                print "\n";
        }

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove duplicate in colomn

i have a file which have two columns, 2nd column have duplicate values and i want to delete and keep only distinct on basis on 2nd column. -bash-4.1$ cat file_re 7440 713543695 7441 713543695 4603 714457614 4602 714457614 40301 717937765 40281 717937765 33741 721208982... (2 Replies)
Discussion started by: mirwasim
2 Replies

2. Shell Programming and Scripting

Switch line in txt file

Hi I have problem with replace line in txt file , I have this string: 144185 DISK Piece qqr8ot6l_1_1 -- 144186 DISK Piece ukr8pf2e_1_1 -- 144187 DISK Piece ter8p9gc_1_1 -- 144188 DISK Piece 4er8qb84_1_1 and (8 Replies)
Discussion started by: primo102
8 Replies

3. Solaris

Switch to su

Hi, I've put the correct root password but why do I get this below? huamin@SOL11I:~$ su Password: su: Sorry huamin@SOL11I:~$ Many Thanks & Best Regards, HuaMin (16 Replies)
Discussion started by: HuaMin
16 Replies

4. Programming

Passing arguments from command line to switch case statement in C

Hi Am pretty new to C.. Am trying to pass the arguments from command line and use them in switch case statement.. i have tried the following #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. Shell Programming and Scripting

how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable

hi, how to access console of a switch having rj45 on switch side to db 9 female on pc side console cable which needs to be connected to one console server having rj11 on its side and db 9 female on other end.i.e. on switch side,console cable has rj45 and db 9 pin female connector on other side of... (1 Reply)
Discussion started by: pankajd
1 Replies

6. Shell Programming and Scripting

How to exclude certain colomn from the file?

Hi I have a file in the following format: 4135 f4135acc: 39798 rmtdb: 0 /t1/data/f4135acc.dta 4135 f4135pdb: 39795 rmtdb: 0 /bb/data/f4135pdb.dta 4135 p4135eng: 0 rmtdb: 0 /bb/bin/p4135eng 4135 r4135eng: 14142 rmtdb: 0 ... (6 Replies)
Discussion started by: aoussenko
6 Replies

7. Shell Programming and Scripting

need help for cp with -p switch

Guys, I need to copy files from source to destination with datetime preserved I did it with cp -p <source>/file <destinaltion>/file But when I do stat command on copied file , it seems the copied file has "change time" modified. Please guide me in understanding (2 Replies)
Discussion started by: mohan_xunil
2 Replies

8. Shell Programming and Scripting

changing colomn to the line

I have a file which has only one colomn of numbers,ex: 122 173 292 400 979 2152 2339 2376 2387 2446 2450 What ksh / unix command should I use to create a file in which those numbers will be in one line,like this 122 173 292 400 979 .... etc Thanks a lot for help (9 Replies)
Discussion started by: aoussenko
9 Replies

9. Programming

Switch

using switch can we match for more than one values.. eg: switcha(a) { case 1, 2, 3: printf("ddd"); break; case 4, 5, 6: printf("mmm"); break; } In this case wat i found was only for the last value, i.e 3 and 6 the switch works. ... (12 Replies)
Discussion started by: abey
12 Replies

10. Shell Programming and Scripting

can you switch

hi, i am try to run following script in c-shell, using switch command. #!/bin/csh choice=0 while do echo "system monitor" echo " 1) system paging 2) system file inf. 3) system disk inf. 9) exit " echo "select an option: \c" read choice case $choice in 1)... (3 Replies)
Discussion started by: neer45
3 Replies
Login or Register to Ask a Question