LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2008, 09:37 PM   #1
fred2008
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Rep: Reputation: 0
AWK - Beginner brainlock


After a day of reading awk and sed tutorials I've hit the wall on a problem and would appreciate advice from the knowledgeable, please. I don't know my arrays,string or variable substitutions from shinola. It all throws me for a loop.

I've sliced and diced a bunch of stuff from different sources using sed and awk one-liners mainly to produce a file for the next step.

The file is sorted on the first field.
The first field is a 4 digit storage location, the second field is the parts in the location, separated by a whitespace.

The file looks likes this:

1234 78adbcd
1234 hj23part
3456 23another
3456 moreparts
3457 another1
3457 yetanother

I'd like to read the file and produce an output file named by field 1, with contents being the part numbers and a count of the parts. I.E. 1234.txt and when you view the file, a count of the parts and a listing of their numbers as in field 2. The order in which the parts appear within the location is not important.

I can't figure out where to begin so that the first field is read just once and all the parts are tossed into the output file until the storage location number changes.

Any clues or pointers with would be appreciated.
 
Old 06-08-2008, 09:49 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
This should do it.
Code:
awk '{ print $2 >> $1 ".txt"; }' input_file
If find it difficult to understand how you can have done a whole day's worth of awk and not be able to write this. Maybe it's just the re-direction partr with the ">>"?
 
Old 06-08-2008, 10:06 PM   #3
fred2008
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
This should do it.
Code:
awk '{ print $2 >> $1 ".txt"; }' input_file
If find it difficult to understand how you can have done a whole day's worth of awk and not be able to write this. Maybe it's just the re-direction partr with the ">>"?
Oh damn, that was easy. I think I overdosed on manuals and was trying to do it the hard way.

Thanks a bunch.
 
Old 06-09-2008, 03:26 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Sorry if I sounded snotty.
 
Old 06-09-2008, 01:43 PM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Actually,
there's no need for double redirection in awk:

Code:
awk '{ print $2 > $1 ".txt" }' input_file
You may have problems not closing the output file though,
so this will be safer:

Code:
awk '!_[$1]++ { close(fn); fn = $1 ".txt" }
{ print $2 > fn }'  input_file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM
a beginner need help ., please mni127 Mandriva 1 10-06-2006 08:32 PM
beginner needs various help. meeep Linux - Software 44 08-21-2005 12:10 AM
I am THE beginner please help me :( statuszero Linux - General 10 05-26-2002 03:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration