Comparing rows and columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing rows and columns
# 1  
Old 04-10-2011
Comparing rows and columns

Hi,

i've a .csv file with the data as below: -

file1.h, 2.0
file2.c, 3.1
file1.h, 2.5
file3.c, 3.3.3
file1.h, 1.2.3

I want to remove the duplicate file names considering only the one with the highest version number..

output should be

file1.h, 2.5
file2.c, 3.1
file3.c, 3.3.3

Is there any way using awk or sed that i can do this?

thanks
prav

Last edited by pravsripad; 04-10-2011 at 11:24 AM.. Reason: added thanks :-)
# 2  
Old 04-10-2011
Code:
for i in `cut -d, -f1 file_name | sort -u`; do sort -rk2 file_name | grep -m1 $i; done

---------- Post updated at 08:05 PM ---------- Previous update was at 08:05 PM ----------

Code:
kamaraj@kamaraj-laptop:~/Desktop/Scripts$ cat interchange 
file1.h, 2.0
file2.c, 3.1
file1.h, 2.5
file3.c, 3.3.3
file1.h, 1.2.3
kamaraj@kamaraj-laptop:~/Desktop/Scripts$ for i in `cut -d, -f1 interchange | sort -u`; do sort -rk2 interchange | grep -m1 $i; done
file1.h, 2.5
file2.c, 3.1
file3.c, 3.3.3

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 04-10-2011
Try:
Code:
 awk '{x=$2;gsub("\\.","",x);for (i=1;i<=(3-length(x));i++){x=x"0"};if (a[$1]<x){a[$1]=x;b[$1]=$0}}END{for (i in b){print b[i]}}' file.csv

Assuming that highest number of digits in the version number is 3. If it is higher, change red "3" to what is suitable for you.
# 4  
Old 04-10-2011
Thanks both of you.

But i could get it to work this way: -

Code:
 
#!/bin/sh
REPEATED_FILES=`cut -f1 -d, test_version_file | sort | uniq -d`
for repeat in $REPEATED_FILES
do
grep "$repeat" test_version_file | sort -rk2 | awk -F, '{if($2 > val) {val=$2;print $0;}}'
done

it's a variation of itkamaraj's code...

@itkamaraj :- grep -m option doesn't work on my machine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Comparing two files and writing mismatched rows along with mismatched columns. Pointing out the mism

I got a requirement where I need to compare two files wrt to each columns and write the corresponding difference in another file along with some identification showing mismatched columns. Pointing out the mismatched columns is my main problem statement. For example we have files like: File 1 ... (8 Replies)
Discussion started by: piyush pankaj
8 Replies

2. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

3. UNIX for Dummies Questions & Answers

Comparing rows

Hi, I have txt file below 02.05.2014,10.05.2014,dfs,srtytr,tyhty 05.10.2014,15.10.2014,456ef,t6y5fgtd,xg45t 21.11.2014,28.11.2014,sefser,dfw344,zsdfrw 22.12.2014,30.12.2014,fwe,aerw4,zwq4q my script will ask two input, enter start date 05.10.2014 enter end date... (3 Replies)
Discussion started by: stew
3 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Comparing columns in a file

I have two files. One a small one and another one is big. The smaller one look like this: Filename: 1.tmp 3453 0 326543 1 2321 0 3212 1 The big file looks like this: Filename 1.res 0.3232 2321 9.2922 123 0.983 3212 8.373 326543 0.9 3453 1.098 3432 I want to extract those lines... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

8. Shell Programming and Scripting

Comparing two columns

Hi, I want to compare two columns and find out missing entries e:g Column 1 Column 2 1 1 2 2 3 13 4 10 19 234 Results woud be 13. I will appreciate very much if anyone help me :). (12 Replies)
Discussion started by: krabu
12 Replies

9. Shell Programming and Scripting

Comparing rows in two tables and sending the differnce to mail

Hi, I have a table ,containg 2 coloumns and many rows,which is updated everyday.The no.of rows in the table changes everyday. i have to write the difference between yesterdays tabtle and todays table to a new file.The row that is new in the todays table need not to be shown.only the change... (2 Replies)
Discussion started by: bab123
2 Replies

10. Shell Programming and Scripting

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (4 Replies)
Discussion started by: ggopal
4 Replies
Login or Register to Ask a Question