The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 09-30-2009
heavenfish heavenfish is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 3
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
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0