Code Comments
Programming Forum and web based access to our favorite programming groups.Not sure if this should be an awk thing... But if I have the following: XX 234234234234 XX 2342345234243 YY 234234234243 YY 23423423423423 ZZ 3245324534534 ZZ 54353453453453 and want to split the XX, YY and ZZ records into separate files (so the two XX records in one file, the two YY records in one file, etc.) , what would be the best way to do this??!! Thanks for helping a complete newb!!! My SED/AWK book is on its way to me in the mail...
Post Follow-up to this message
awknewb wrote:
> Not sure if this should be an awk thing...
> But if I have the following:
> XX 234234234234
> XX 2342345234243
> YY 234234234243
> YY 23423423423423
> ZZ 3245324534534
> ZZ 54353453453453
>
> and want to split the XX, YY and ZZ records into separate files (so the
> two XX records in one file, the two YY records in one file, etc.) , what
> would be the best way to do this??!!
> Thanks for helping a complete newb!!! My SED/AWK book is on its way to me
> in the mail...
>
awk '{print >> $1}'
Ed.
Post Follow-up to this messageIn article < 1dec3354172592c05f7a1aa89c85ff42@localho
st.talkaboutprogramming.
com>,
awknewb <nospam@nospam.hfx.eastlink.ca> wrote:
>Not sure if this should be an awk thing...
>But if I have the following:
>XX 234234234234
>XX 2342345234243
>YY 234234234243
>YY 23423423423423
>ZZ 3245324534534
>ZZ 54353453453453
>
>and want to split the XX, YY and ZZ records into separate files (so the
>two XX records in one file, the two YY records in one file, etc.) , what
>would be the best way to do this??!!
I suppose something like:
#!gawk
{ print > ($1 ".file") }
Note that gawk is somewhat relevant here, because most AWKs (including
TAWK), have a limit of (usually) 10 concurrent open files. Although not
relevant in the instant case (where n=3), it could be relevant down the
road.
Post Follow-up to this messageI'm... In either command that you gus suggest, where do I put the input file name? Sorry if that is a dumb question.....
Post Follow-up to this messageawknewb wrote: > I'm... > In either command that you gus suggest, where do I put the input file > name? > Sorry if that is a dumb question..... > At the end of the line: awk '...' inputfile or piped in: whatevercommand inputfile | awk '...' Better put a rush on that awk book ;-). Ed.
Post Follow-up to this message>> Better put a rush on that awk book ;-).
tell me about it!
OK, one more, of course.
I now have the two commands working how I want:
awk '{print >> $1}' file.txt
splits the files up for me, and:
The following command cuts out the leading data for me that I don't need:
cut -c58- file.txt > newfile.txt
SO, how can I combine these two commands to split the file up (based on
the first field, so can't do the cut first), and then cut the data out?
Can it be strung together in one command?
Post Follow-up to this messageEmample:
awk '{print(substr($0,4)) >> $1}'
Input:
XX 234234234234
XX 2342345234243
YY 234234234243
YY 23423423423423
ZZ 3245324534534
ZZ 54353453453453
Output in the file XX:
234234234234
2342345234243
Try it with awk '{print(substr($0,58)) >> $1}' and you will get
3 files XX, YY and ZZ only with the substring 58-End.
Stefan
Post Follow-up to this messageIn article < 0eb5d1065855f95749a3214d850af90d@localho
st.talkaboutprogramming.
com>,
awknewb <nospam@nospam.hfx.eastlink.ca> wrote:
>awk '{print >> $1}' file.txt
This should be ">", not ">>". Trust me on this.
>splits the files up for me, and:
>The following command cuts out the leading data for me that I don't need:
>
>cut -c58- file.txt > newfile.txt
>
>SO, how can I combine these two commands to split the file up (based on
>the first field, so can't do the cut first), and then cut the data out?
>Can it be strung together in one command?
Of course. And doing so keeps you on-topic for this newsgroup (an added
bonus!)
Assuming you really want to delete the first 57 chars, you would do:
{ print substr($0,58) > $1 }
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.