Home > Archive > AWK > March 2004 > One more awk question (I think)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
One more awk question (I think)
|
|
| awknewb 2004-03-26, 11:09 pm |
| 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...
| |
| Ed Morton 2004-03-26, 11:09 pm |
|
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.
| |
| Kenny McCormack 2004-03-26, 11:09 pm |
| In 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.
| |
| awknewb 2004-03-26, 11:09 pm |
| I'm ...
In either command that you gus suggest, where do I put the input file
name?
Sorry if that is a dumb question.....
| |
| Ed Morton 2004-03-26, 11:09 pm |
|
awknewb 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.
| |
| awknewb 2004-03-26, 11:09 pm |
| >> 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?
| |
| Stefan Lagotzki 2004-03-26, 11:09 pm |
| Emample:
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
| |
| Kenny McCormack 2004-03-26, 11:09 pm |
| In 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 }
|
|
|
|
|