Last field problem while comparing two csv files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Last field problem while comparing two csv files
# 1  
Old 06-12-2008
Error Last field problem while comparing two csv files

Hi All,

I've two .csv files as below
file1.csv
abc, tdf, 223, tpx
jgsd, tex, 342, rpy
a, jdjdsd, 423, djfkld


Where as file2.csv is the new version of file1.csv with some added fields in the end of each line and some additional lines.
lfj, eru, 98, jkldj, 39, jdkj9
abc, tdf, 223, tpx, 4sdd, 43
jgsd, tex, 342, rpy, 343js, j88
a, jdjdsd, 423, djfkld, djd, 322i
djlfj, djd, 499, djld, 323u, d88l
fjdkl, jdfsld, 45k, djdkl, 343, 334p


Now my requirement is to get only these additional lines from file2.csv.
I tried with 'diff' and 'comm'. Due to added additional fields I am not able to get the desired output as below:
lfj, eru, 98, jkldj, 39, jdkj9
djlfj, djd, 499, djld, 323u, d88l
fjdkl, jdfsld, 45k, djdkl, 343, 334p


Even I am still trying to solve this myself. Any help would be much appreciated from any of friends...

Thanks in advance / Mysore Ganapati
# 2  
Old 06-12-2008
You can try something like this, but it may fail in some situations.

Code:
fgrep -f file1.csv -v file2.csv

# 3  
Old 06-12-2008
fgrep Not worked for my problem

I gave you the sample of files. But in reality, both files are having 10 millions of records. When I tried your solution using fgrep, got the below error message:
fgrep: could not allocate memory for wordlist

Are there any other solutions please...
# 4  
Old 06-12-2008
Hm,
if the number of columns in the first csv is fixed,
you could try Awk:

Code:
awk -F, 'NR == FNR {
  _[$0]
  next
  }
!(($1 FS $2 FS $3 FS $4) in _)
' file1.csv file2.csv

But you may encounter similar problems ...

Or, something like this, will not be fast Smilie:

Code:
awk -F, 'BEGIN {
  f = ARGV[1]
  ARGV[1] = ""
  }
{ 
  while ((getline _f < f) > 0)
    if (($1 FS $2 FS $3 FS $4) == _f) 
	  next
	close(f)
  }
1' file1.csv file2.csv

You could modify the second example and use it even if the number of columns in the first file is variable.

Last edited by radoulov; 06-12-2008 at 09:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two CSV files

I have two csv files and im trying to compare them. e.g. SAMPLE DATA: file one: ZipCode Name 20878 Washington 10023 Missouri 20304 Maryland file two: ID Name City ZipCode 11654 ... (11 Replies)
Discussion started by: dan139
11 Replies

2. Shell Programming and Scripting

Comparing two files field and position

I have two files : file1 and file2 file1 format: 7026-H70 7026-1017685 7026-H70 7026-1017687 7026-B80 7026-108D65A 7026-B80 7026-108D67A 7046-B50 7026-1034B4A File2 format : mt01cp01 7026-B80 01108D69A mt01cp02 7026-B80 01108D68A mt01sv01 7046-B50... (7 Replies)
Discussion started by: amir07
7 Replies

3. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

4. Shell Programming and Scripting

Comparing two large unsorted csv files

Hi All, My requirement is to write a shell script to compare two large csv files. I've created sample files for explaining my problem i.e., a.csv and b.csv contents of files: ----------------- a.csv ------ Type,Memory (Kb),Location HD,Size (Mb),Serial # XT,640,D402,0,MG0010... (2 Replies)
Discussion started by: vasavi
2 Replies

5. Shell Programming and Scripting

Comparing 2 CSV files and sending the difference to a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 ... (1 Reply)
Discussion started by: Naresh101
1 Replies

6. Shell Programming and Scripting

Comparing 2 difference csv files

Hello, I have about 10 csv files which range from csv1 - csv10. Each csv file has same type/set of tabs and we have around 5-6 tabs for each of the csv file which have slightly different content(data). A sample of CSV1 is shown below: Joins: Data related to Joins, it can be any number of... (2 Replies)
Discussion started by: bobby1015
2 Replies

7. Shell Programming and Scripting

comparing csv files

Hi! I'm just new to shell scripting n simple tasks looks so tough in initial stage. i need to write a script which will read a property file, property file will be containing count of the csv files, and in a folder(same folder) there will be respective csv files. like Property file data1=100... (3 Replies)
Discussion started by: sukhdip
3 Replies

8. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

9. Shell Programming and Scripting

comparing files with field using awk

hi, i have 1 files a.csv temp.out a.cvs looks like add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-209 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-219 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-220 add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-221 and temp.out looks... (1 Reply)
Discussion started by: raghavendra.cse
1 Replies

10. Shell Programming and Scripting

Field comparing in files

Guys trying to compare field in two files. For Ex: demo.txt 23.33.4.2 hostname 3.2.4.2 hostname12 demo1.txt 3.3.3.3 hostname23 45.23.23.23 hostname 323 I would like to compare the ips b/w these two files.any... (2 Replies)
Discussion started by: coolkid
2 Replies
Login or Register to Ask a Question