Compare fields and keep record with bigger ID?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare fields and keep record with bigger ID?
# 1  
Old 02-19-2013
Compare fields and keep record with bigger ID?

How do you write a shell script to compare records with the same fields then keep the biggeer id number fields (field separate by a pipe)
Code:
1150|	San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
1263|San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
2001|	Tampa|4|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000
2002|Tampa|8|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000

and the results should be
Code:
1263|San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
2001|	Tampa|4|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000
2002|Tampa|8|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000

Thanks,
# 2  
Old 02-19-2013
What fields the same?
Looks like you are trying to compare on the second (city name) field, with the first combination on San Jose. But, what about Tampa?
# 3  
Old 02-19-2013
Code:
1150|	San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
1263|San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000

These 2 records have the same all fields
San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
but only record ID is different so i would like to keep record with bigger id
--> 1263|San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000
Code:
2001|	Tampa|4|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000
2002|Tampa|8|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000

as long any field different even it the same Tampa but we will keep all.
Code:
2001|	Tampa|4|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000
2002|Tampa|8|15|6|2013-02-20 00:00:00.000|2013-02-21 00:00:00.000

Thanks
# 4  
Old 02-20-2013
Okay I made it work now with this code
Code:
awk -F"|" '$1>=highest[$2,$3,$4,$5,$6,$7]{maxrec[$2,$3,$4,$5,$6,$7]=$0;highest[$2,$3,$4,$5,$6,$7]=$1} END{for(i in maxrec)print maxrec[i]}' filename.txt | sort -t\| -k1

Thanks
# 5  
Old 02-20-2013
Code:
 
sort -nr | line

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

3. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Shell Programming and Scripting

zsh compare size pdf and delete bigger?

I have used an script to reduce the size of multiples pdf. This script creates files with the same name but with different extension. The extension of the compressed files is xpdf. Sometimes the "compressed" xpdf are bigger than the "uncompressed"pdf. I want to create a zsh script to compare each... (1 Reply)
Discussion started by: AMDx64BT
1 Replies

5. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

6. Shell Programming and Scripting

Making changes in the fields of a record

:confused: Hi Friends, In the record below i have to make changes in the fields by putting the values stored in the temporary variables, x, y, z, p, q, r: 2) In the TBT record store the values in the various fields as: a) X in a field position 51 to 56 b) Y... (5 Replies)
Discussion started by: kanu_pathak
5 Replies

7. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

8. Shell Programming and Scripting

Manipulating fields record wise

Hi all, I have an input file with no delimiter. Let us say the file is abc.txt having values for fields namely, EmpNumEnameDesigSalDept. Ofcourse the file has got several records. Every field has got a fixed start and end position. I need to assign the fields to corresponding varibles say... (1 Reply)
Discussion started by: rinku11
1 Replies

9. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies

10. Shell Programming and Scripting

How to screen each variable record for right # of fields

Hi, In a script I have to check that input text files with a variable number of tab delimited fields have at least n fields and no more than m fields. Records are delimited by <CR> and <LF>. I have figured out code that will strip out all the alpha-numeric characters, convert the tabs to... (3 Replies)
Discussion started by: jvander
3 Replies
Login or Register to Ask a Question