*BSD News Article 10575


Return to BSD News archive

Received: by minnie.vk1xwt.ampr.org with NNTP
	id AA321 ; Sun, 31 Jan 93 14:02:57 EST
Xref: sserve comp.unix.bsd:10628 comp.unix.misc:6305 comp.unix.shell:7582 comp.unix.questions:30753
Path: sserve!manuel.anu.edu.au!munnari.oz.au!spool.mu.edu!uunet!lhdsy1!nntpserver.chevron.com!gruc19.nor.chevron.com!hjiwa
From: hjiwa@gruc19.nor.chevron.com (Jeff Wang)
Newsgroups: comp.unix.bsd,comp.unix.misc,comp.unix.shell,comp.unix.questions
Subject: Re: Rotating a file (row <-> columns). HELP WANTED
Message-ID: <1993Jan30.150622@gruc19.nor.chevron.com>
Date: 29 Jan 93 21:06:22 GMT
References: <C1IxwG.2FM@inews.Intel.COM>
Sender: news@nntpserver.chevron.com (USENET News System)
Organization: Chevron Petroleum Technology Company
Lines: 83

vdalvi@mcd.intel.com (Vishram Dalvi ~) writes:
|> Hi Netters,
|> 	I am sure that this problem must have been asked before and
|> that some one may already have a solution to it. I have a file which
|> has a whole bunch of names listed in successive columns. I want to
|> be able to read them serially. All the names are not the same length
|> and may be separated by one or more spaces. Currently I am turning
|> the file around by cutting successive columns and then using "echo -n"
|> for each entry in that columns. This is ssssllloooww :-( Does the file
|> HAVE to be turned around to retrieve the names ? Can it be speeded up
|> some way or avoided altogether ? (short of not running the program :-) )
|> Here is what the input looks like:
|>                                                   
|>       c                                           
|>       o c    c                                    
|> c c   l o    o c                                   
|> o o   u l    l o                                  
|> l l   m u    u l                                  
|> u u   n m    m u                                  
|> m m   t n    n m                                  
|> n n   h f    f n                                  
|> o t   r o    i s                                  
|> n w   e u    v i                                  
|> e o   e r    e x                                    
|> 
|> I want to turn it into:
|> 
|> columnone
|> columntwo
|> columnthree
|> columnfour
|> columnfive
|> columnsix

Here's a little awk script that rotates rows and columns and transposes a
matrix of values that are delimited by spaces...

R1C1 R1C2  R1C3
R2C1 R2C2  R2C3
R3C1 R3C2  R3C3
R4C1 R4C2  R4C3

becomes -

R1C1 R2C1 R3C1 R4C1
R1C2 R2C2 R3C2 R4C2
R1C3 R2C3 R3C3 R4C3

--------------------CUT-HERE--------------------
#!/bin/sh5
## TRANSPOSE A MATRIX
## ASSUMES ALL LINES HAVE THE SAME NUMBER OF FIELDS
## USAGE : transpose file                                                      #

exec awk '
NR == 1 {
  n = NF
  for (i = 1; i <= NF; i++)
    row[i] = $i
  next
}
{
  if (NF > n)
    n = NF
  for (i = 1; i <= n; i++)
    row[i] = row[i] " " $i
}
END {
  for (i = 1; i <= n; i++)
    print row[i]
}' ${1+"$@"}
--------------------CUT-HERE--------------------

Hope that helps.

-- 
 #====}==) #===(==}  #====}==) #===(==}  {==)===# (=={====#  {==)===# (=={====#
>> Jeff Wang                             Net   : hjiwa@gruc19.nor.chevron.com <<
>> Chevron Petroleum Technology Company  Profs : hjiwa (hovmb)                <<
>> Geophysical/Geological Applications   Phone : (504) 592-6162               <<
>> 935 Gravier Street, Room 1006         Fax   : (504) 592-6958               <<
>> New Orleans, Louisiana 70112          Valeo,vales. Die dulci fruimini!! :) <<
 #===(==}      What if no one ever asked a hypothetical question?      {==)===#