The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Column sum group by uniq records Nayanajith Shell Programming and Scripting 17 05-20-2009 09:57 AM
To find the count of records from tables present inside a file. navojit dutta Shell Programming and Scripting 1 08-29-2007 11:04 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
Help comparing 2 files to find deleted records eja UNIX for Dummies Questions & Answers 2 04-03-2007 08:53 AM
How to find All Primary and Secondary Group ID's for a user sanjay92 UNIX for Dummies Questions & Answers 2 03-27-2002 02:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-29-2007
thumsup9 thumsup9 is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 97
find and group records in a file

Hi,
I have this file which has 3 columns, District , stores and unit. What I want is all rows belonging to one district to be created separately under each district, the districts may vary every day , the source file may have 3 districts today and may have 160 tomorrow, so what I need is a script that puts all district rows in one file and so on , do you have any ideas ? Attached is the sample of what I am asking for


Source File
District Store UnitSales
D1 ST101 4
D1 ST102 2
D1 ST103 3
D2 ST104 45
D2 ST105 6
D2 ST106 7
D1 ST107 8
D1 ST108 89
D2 ST109 8
D3 ST111 0
D3 ST101 7


D1 file
D1 ST101 4
D1 ST102 2
D1 ST103 3
D1 ST107 8
D1 ST108 89

D2 file
D2 ST104 45
D2 ST105 6
D2 ST106 7
D2 ST109 8

D3 file
D3 ST111 0
D3 ST101 7

Thanks, very much
  #2 (permalink)  
Old 01-29-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
Code:
while read district store unit; do
    echo "$district $store $unit" >> ${district}.txt
done < file
  #3 (permalink)  
Old 01-29-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try....
Code:
awk 'NF{print $0 > "outfile." $1}' infile
  #4 (permalink)  
Old 02-05-2007
alfredo123 alfredo123 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 44
Quote:
Originally Posted by Ygor
Try....
Code:
awk 'NF{print $0 > "outfile." $1}' infile
I did not understand how this works??

Could you please explain a little bit more?
  #5 (permalink)  
Old 02-05-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
http://www.cs.uu.nl/~piet/docs/nawk/nawk_37.html#SEC40
  #6 (permalink)  
Old 02-06-2007
alfredo123 alfredo123 is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 44
I understand the redirect of the record to outfile.field1 works...

I dont know how NF(no of fields) is applicable here... and also how they get grouped is what I wanted to know.
  #7 (permalink)  
Old 04-18-2007
mike_ap2006 mike_ap2006 is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 12
How Can I Aggregate A Field In A File

I Have a file

34EMEA|VODAFONE ES|5484|CPN|04/01/07|1|300
34LA|VODAFONE ES|5484|CPN|04/01/07|1|300

how can i aggregate it to
34EMEA|VODAFONE ES|5484|CPN|04/01/07|1|600

awk -F"|" '{ sum[$4] += $7 } END { for (name in sum) print name, sum[name] }' data_file

this aggregarates if column 4 is similar. but if columns 2 to 5 are similare i want to aggregate.
so when i tried
awk -F"|" '{ sum[$2]sum[$3]sum[4] += $7 } END { for (name in sum) print name, sum[name] }' data_file

it is not working. can u please help.
Many thanks
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0