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
# 8  
Old 02-13-2007
Don't you mean that NF > 0. Also I would change it slightly to be more robust
awk 'NR > 1 && NF == 3 { print $0 > $1".txt"}' this will also take care of usual garbage input lines
# 9  
Old 04-18-2007
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
# 10  
Old 04-18-2007
Code:
awk -F"|" '{ sum[$2 FS $3 FS $4] += $7 } END { for (name in sum) print name, sum[name] }' data_file

# 11  
Old 04-18-2007
Great!!

Vgersh,
WOW!!! Thats great and helped me..

but when i use this command,

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

it prints only fields 2 to 7, i m not able to print first filed. I tried
...print $1"|"name"|"sum[name] }' data_file but is not working..
could you please help. Thanks a million..
# 12  
Old 04-18-2007
Code:
sum[$1 FS $2 FS $3 FS $4 FS $5]

# 13  
Old 04-18-2007
Hi Vgersh,

Thanks for your response. But the code

sum[$1 FS $2 FS $3 FS $4 FS $5] will compare fields from 1 to 5.

I need to comapre fields from 2 to 5 and aggregate, while printing i should print the first field also. The first field can be anyone within the matching lines (lines having similar value from 2nd to 5th coulmn)

23ASIA|VODAFONE ES|5484|000000000000314521|03/31/07|1|100
56NA|VODAFONE ES|5484|000000000000314521|03/31/07|1|200

then the output should be

either 23ASIA|VODAFONE ES|5484|000000000000314521|03/31/07|1|300
OR 56NA|VODAFONE ES|5484|000000000000314521|03/31/07|1|300

can we acheive this?
# 14  
Old 04-18-2007
Code:
awk -F"|" '{ idx=$2 FS $3 FS $4 FS $5; sum[idx] += $7; first[idx]=$1 } END { for (name in sum) printf("%s%s%s%s%s\n", first[name], FS, name, FS, sum[name]) }' data_file

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