Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

One more awk question (I think)
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...


Report this thread to moderator Post Follow-up to this message
Old Post
awknewb
03-27-04 04:09 AM


Re: One more awk question (I think)

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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-27-04 04:09 AM


Re: One more awk question (I think)
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-27-04 04:09 AM


Re: One more awk question (I think)
I'm ...
In either command that you gus suggest, where do I put the input file
name?
Sorry if that is a dumb question.....


Report this thread to moderator Post Follow-up to this message
Old Post
awknewb
03-27-04 04:09 AM


Re: One more awk question (I think)

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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-27-04 04:09 AM


Re: One more awk question (I think)
>> 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?




Report this thread to moderator Post Follow-up to this message
Old Post
awknewb
03-27-04 04:09 AM


Re: One more awk question (I think)
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

Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Lagotzki
03-27-04 04:09 AM


Re: One more awk question (I think)
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 }


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-27-04 04:09 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:49 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.