From: Willie Wong <wwong@Princeton.EDU>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] [OT] - command line read *.csv & create new file
Date: Sun, 22 Feb 2009 15:59:39 -0500 [thread overview]
Message-ID: <20090222205938.GA455@princeton.edu> (raw)
In-Reply-To: <5bdc1c8b0902221106h71a8783y698aa209ace59a6@mail.gmail.com>
On Sun, Feb 22, 2009 at 11:06:31AM -0800, Penguin Lover Mark Knecht squawked:
> I've got a really big data file in essentially a *.csv format.
> (comma delimited) I need to scan this file and create a new output
> file. I'm wondering if there is a reasonably easy command line way of
> doing this using something like sed or awk which I know nothing about.
> Thanks in advance.
Definitely more than doable in sed or awk. If you want a reference
book, try http://oreilly.com/catalog/9781565922259/
Unfortunately I haven't used awk in the longest time and can't
remember how it will go. The following sed recipe may work, modulo
some small modifications
> The basic idea goes something like this:
>
> 1) The input file might look this the following where some of it is
> attributes (shown as letters) and other parts are results. (shown as
> numbers)
>
> A,B,C,D,1
> E,F,G,H,2
> I,J,K,L,3
> M,N,O,P,4
> Q,R,S,T,5
> U,V,W,X,6
>
> 2) From the above data input file I want to take the attributes from a
> few preceeding lines (say 3 in this example) and write them to the
> output file along with the result on the last of the 3 lines. The
> output file might look like this:
>
> A,B,C,D,E,F,G,H,I,J,K,L,3
> E,F,G,H,I,J,K,L,M,N,O,P,4
> I,J,K,L,M,N,O,P,Q,R,S,T,5
> M,N,O,P,Q,R,S,T,U,V,W,X,6
>
> 3) This must be done as a read/process/write operation of some sort
> because the input file may be far larger than system memory.
> (Currently it isn't, but it likely will eventually be.)
>
> 4) In my example above I suggested that there is a single result but
> their may be more than one. (Don't know yet.) I showed 3 lines but
> might be doing 10. I don't know. It's important to me to pick a
> moderately flexible way of dealing with this as the order of columns
> and number of results will likely change over time and I'll certainly
> need to adjust.
First create the sedscript
sedscript1:
--------------------------
1 {
N
N
}
{
p
D
N
}
--------------------------
The first block only hits when the first line of input is read. It
forces it to read the next two lines.
The second block hits for every pattern space, it prints the three
line blocks, deletes the first line, and reads the next line.
Now create the sedscript
sedscript2:
--------------------------
{
N
N
s/,[^,]\n/,/gp
d
}
--------------------------
This reads a three-line block at a time, removes the last field (and
the new line character) from all but the last line, replacing it with
a comma. Then it prints. And then it clears the pattern space.
So you can do
cat INPUT | sed -f sedscript1 | sed -f sedscript2
should give you what you want. Like I said, the whole thing can
probably be done a lot more eloquently in awk. But my awk-fu is not
what it used to be.
For a quick reference for sed, try
http://www.grymoire.com/Unix/Sed.html
W
--
Ever stop to think, and forget to start again?
Sortir en Pantoufles: up 807 days, 18:51
next prev parent reply other threads:[~2009-02-22 20:56 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-22 19:06 [gentoo-user] [OT] - command line read *.csv & create new file Mark Knecht
2009-02-22 20:15 ` Etaoin Shrdlu
2009-02-22 22:28 ` Mark Knecht
2009-02-22 22:57 ` Etaoin Shrdlu
2009-02-22 23:31 ` Mark Knecht
2009-02-23 6:17 ` Paul Hartman
2009-02-23 9:57 ` Etaoin Shrdlu
2009-02-23 16:05 ` Mark Knecht
2009-02-23 22:18 ` Etaoin Shrdlu
2009-02-24 2:26 ` Mark Knecht
2009-02-24 10:56 ` Etaoin Shrdlu
2009-02-24 14:41 ` Mark Knecht
2009-02-24 17:48 ` Etaoin Shrdlu
2009-02-24 22:51 ` Mark Knecht
2009-02-25 10:27 ` Etaoin Shrdlu
2009-02-22 20:59 ` Willie Wong [this message]
2009-02-22 23:15 ` Mark Knecht
2009-02-23 0:57 ` Willie Wong
2009-02-23 1:54 ` Mark Knecht
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=20090222205938.GA455@princeton.edu \
--to=wwong@princeton.edu \
--cc=gentoo-user@lists.gentoo.org \
/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