![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| transpose file | new_ds_man | Shell Programming and Scripting | 3 | 07-09-2009 01:45 AM |
| How to Read the entire file using while loop | sdosanjh | Shell Programming and Scripting | 11 | 05-22-2009 08:46 AM |
| how to restore an entire system from a tar file? | omd | SUN Solaris | 1 | 12-04-2008 06:31 PM |
| Remove spaces from first field, and write entire contents into other text file | carriehoff | Shell Programming and Scripting | 3 | 11-11-2008 02:45 PM |
| Editing the end of the file without loading the entire file | Garuda | UNIX for Advanced & Expert Users | 3 | 06-22-2004 06:23 PM |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Transpose an entire text file
Hello all, I want to transpose the rows of a file to the columns (every characters include spaces), i.e.: input: Code:
abcdefg 123 456 output: Code:
a1 b2 c3 d e4 f5 g6 I wrote a script: Code:
#!/bin/csh -f set num=`cat $1 |wc -l` echo "$num rows of $1 transposing to columns..." if ( -f temp1 ) then rm temp1 touch temp1 else touch temp1 endif set i=1 set j=2 while ($i <= $num) # read a row, transpose to a column sed -n "$i p" $1 | sed 's/\n*/\n/g' > temp # merge columns in files paste temp$i temp > temp$j rm temp$i @ i++ @ j++ end mv temp$i $1.transposed rm temp echo "Done!" It works, but the paste command has several problems: It inserts an empty column when appends a new column; The columns length need be the same; And the biggest problem is since the script keeps read and write temp files, it is very slow, the files I want to transpose have tens of thousands of rows, so it takes nearly an hour to proceed large files. Can anyone provide a better idea to me? Thanks. Last edited by Franklin52; 09-30-2009 at 05:27 AM.. Reason: Please use code tags! |
| Bookmarks |
| Tags |
| awk, matrix shift, transpose |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|