find and group records in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find and group records in a file
# 1  
Old 01-29-2007
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  
Old 01-29-2007
Code:
while read district store unit; do
    echo "$district $store $unit" >> ${district}.txt
done < file

# 3  
Old 01-29-2007
Try....
Code:
awk 'NF{print $0 > "outfile." $1}' infile

# 4  
Old 02-05-2007
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  
Old 02-05-2007
# 6  
Old 02-06-2007
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  
Old 02-06-2007
Quote:
Originally Posted by alfredo123
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.
Code:
NF{print $0 > "outfile." $1}

The block is executed if NF is greater than one.In case of blank line NF is zero and the block is not executed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To group records in the file in UNIX

Hi All, I am using RHEL 6.9. I got a requirement to group the records in a file.The file content as shown below. #### FAILED JOBS IN XXX ##### 1> ABCD failed in the project XXX 2> HJK Job is in compiled state in the project XXX 3> ILKD failed in the project XXX 4> DFG failed in the... (5 Replies)
Discussion started by: ginrkf
5 Replies

2. Shell Programming and Scripting

Script to find blank records in a file except for few columns

I have a file with the following format: X|High|2|GIC|DM||XHM|||6 Months X|Moderate|2|GIC|DM||XHM|||6 Months X|High|2|GCM|DM||XSF|||6 Months X|Med|2|GCM|DM||XSF|||6 Here there are ten columns but I need to print rows having blank records in any of the rows (except for 6th,8th and 9th... (10 Replies)
Discussion started by: chatwithsaurav
10 Replies

3. UNIX for Dummies Questions & Answers

Find a file in group of jars

Hi, jar -tvf tools.jar | grep LinkInfoImpl Output: 4968 Fri Feb 22 02:34:52 EST 2008 com/sun/tools/doclets/formats/html/LinkInfoImpl.class However, i need the similar output for all jar files under /apps/lib/ directory and its subdirectories. Can you please help format my command to... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

5. UNIX for Dummies Questions & Answers

How to find particular records in a file?

Hi all, i have one file contains 20 records 1 2 - - - 20 i want to find 5th records and 8th records and 14th plz tell me which command use? Thanks to advance (4 Replies)
Discussion started by: venkatreddy
4 Replies

6. Shell Programming and Scripting

Find Duplicate records in first Column in File

Hi, Need to find a duplicate records on the first column, ANU4501710430989 0000000W20389390 ANU4501710430989 0000000W67065483 ANU4501130050520 0000000W80838713 ANU4501210170685 0000000W69246611... (3 Replies)
Discussion started by: Murugesh
3 Replies

7. Shell Programming and Scripting

how to group records in a file

hi, I have records like this D127@dm.com,127,569,BRAD,25/08/2009 23:59 D127@dm.com,127,569,BRAD,25/08/2009 23:59 D159@dm.com,159,1170,DAVE,25/08/2009 23:59 D159@dm.com,159,1181,HALE,25/08/2009 23:59 D393@dm.com,393,1209,CAPIT,25/08/2009 23:59 D457@dm.com,457,571,NORTT,25/08/2009 23:59... (4 Replies)
Discussion started by: trichyselva
4 Replies

8. Shell Programming and Scripting

KSH to group records in a file and compare it with another file

Hi, I've a file like below: DeptFile.csv DeptID EmpID ------- ------ Dep01 Emp01 Dep01 Emp02 Dep01 Emp03 Dep02 Emp04 Dep02 Emp05 I've another file which has EmpFile.csv EmpID Salary ------ ------ (3 Replies)
Discussion started by: Matrix2682
3 Replies

9. Shell Programming and Scripting

find out duplicate records in file?

Dear All, I have one file which looks like : account1:passwd1 account2:passwd2 account3:passwd3 account1:passwd4 account5:passwd5 account6:passwd6 you can see there're two records for account1. and is there any shell command which can find out : account1 is the duplicate record in... (3 Replies)
Discussion started by: tiger2000
3 Replies

10. Shell Programming and Scripting

How to find Duplicate Records in a text file

Hi all pls help me by providing soln for my problem I'm having a text file which contains duplicate records . Example: abc 1000 3452 2463 2343 2176 7654 3452 8765 5643 3452 abc 1000 3452 2463 2343 2176 7654 3452 8765 5643 3452 tas 3420 3562 ... (1 Reply)
Discussion started by: G.Aavudai
1 Replies
Login or Register to Ask a Question