From: Tharaneedharan Vilwanathan <vdharani@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] rows to cols?
Date: Fri, 13 Nov 2009 01:36:43 -0800 [thread overview]
Message-ID: <dac0a5820911130136m791e981cxbfd478cafec0ddae@mail.gmail.com> (raw)
In-Reply-To: <18087f99b33ebe5fbec227e86218045e@hamnavoe.com>
[-- Attachment #1.1: Type: text/plain, Size: 1242 bytes --]
hi,
i rustled up a small limbo program (attached) that does the trick.
hope this helps.
% cat num1.txt
one two three
four five six
seven eight nine
% ./trans num1.txt
one four seven
two five eight
three six nine
% cat num2.txt
one two three four
five six seven
eight nine
ten
eleven twelve
thirteen fourteen fifteen
sixteen seventeen eighteen nineteen
% ./trans num2.txt
one five eight ten eleven thirteen sixteen
two six nine ---- twelve fourteen seventeen
three seven ---- ---- ---- fifteen eighteen
four ---- ---- ---- ---- ---- nineteen
%
thanks
dharani
On Fri, Nov 13, 2009 at 12:44 AM, Richard Miller <9fans@hamnavoe.com> wrote:
>
> > Is there an easy way to transpose the text so that rows become
> > columns, and vice versa? Delimiter is space.
>
> If you know in advance the number of rows & colums, it's easy:
>
> term% cat t
> one two three four
> five six seven eight
> nine ten eleven twelve
> term% tr -s ' ' '\xA' <t | pr -t -3 -l4 | tr -s ' ' ' '
> one five nine
> two six ten
> three seven eleven
> four eight twelve
>
>
[-- Attachment #1.2: Type: text/html, Size: 3106 bytes --]
[-- Attachment #2: trans.b --]
[-- Type: application/octet-stream, Size: 1064 bytes --]
implement Trans;
include "sys.m";
sys: Sys;
include "draw.m";
include "bufio.m";
MaxList : con 500;
Trans: module
{
init: fn(nil: ref Draw->Context, args: list of string);
};
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
if (len args != 2) {
sys->print("Error! File too big\n");
exit;
}
File := hd tl args;
bufio := load Bufio Bufio->PATH;
Iobuf : import bufio;
lc := bufio->open(File, bufio->OREAD);
MaxRow := 0;
MaxCol := 0;
List := array [500] of list of string;
i := 0;
while (1) {
text := lc.gets('\n');
if (text == nil) break;
Count := 0;
(Count, List[i++]) = sys->tokenize(text, " \n");
if (MaxCol < Count) {
MaxCol = Count;
}
MaxRow++;
if (MaxRow >= MaxList) {
sys->print("Error! File too big\n");
exit;
}
}
for (i = 0; i < MaxCol; i++) {
for (j := 0; j < MaxRow; j++) {
#if (List[j] == nil) continue;
if (List[j] == nil) {sys->print("%9s ", "----"); continue;}
sys->print("%9s ", hd List[j]); List[j] = tl List[j];
}
sys->print("\n");
}
}
next prev parent reply other threads:[~2009-11-13 9:36 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-13 7:39 Peter A. Cejchan
2009-11-13 8:44 ` Richard Miller
2009-11-13 9:36 ` Tharaneedharan Vilwanathan [this message]
2009-11-13 23:53 ` dave.l
2009-11-14 9:43 ` Richard Miller
2009-11-14 11:27 ` dave.l
2009-11-14 4:43 ` Lyndon Nerenberg (VE6BBM/VE7TFX)
[not found] <<372549f4a1e93be164e3f5016203c1c3@yyc.orthanc.ca>
2009-11-14 15:11 ` erik quanstrom
2009-11-14 23:23 ` Lyndon Nerenberg (VE6BBM/VE7TFX)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=dac0a5820911130136m791e981cxbfd478cafec0ddae@mail.gmail.com \
--to=vdharani@gmail.com \
--cc=9fans@9fans.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).