Duplicate rows in CSV files based on values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Duplicate rows in CSV files based on values
# 8  
Old 04-19-2011
Quote:
Originally Posted by DGPickett
Is this csv code robust against quoted commas?
DGPickett,

The code does not handle quoted commas.

It works for what was specified in the original post.

We have to remember that the solutions here are designed to solve
the original post description and stated rules.

An experienced IT professional can always propose “what-if” cases
that may cause a code to break - consider the following situations:

Input:
Code:
field1, field2, “last1, first1”
field1, field2, “last2, first2”
fieldA, fieldB, last1
fieldA, fieldB, first1
fieldA, fieldB, last2
fieldA, fieldB, first2

According to the post rules, the output would be:
Code:
field1, field2, “last1, first1, last2, first2”
fieldA, fieldB, “last1, first1, last2, first2”

There could be quoted comma in the first two fields:
Code:
"Last1, First1", "Last2, First2", Value

Also, how would quotation inside of quoted commas would work:

Input:
Code:
field1, field2, “His Answer: \“Yes, I do.\””

or:
Code:
field1, field2, “His Answer: ““Yes, I do.”””

As you can see, there are several other instances of “what-ifs” where the solution could become
very complex and thus not practical for the purpose of solving the original post where the member
does not have such cases.
# 9  
Old 04-19-2011
Quote:
Originally Posted by Shell_Life
An experienced IT professional can always propose “what-if” cases that may cause a code to break
Such as someone editing a code solution in Microsoft Word, adding surplus linebreaks and converting ordinary quotes into special “ ” quotes. Smilie
# 10  
Old 04-19-2011
Quote:
Originally Posted by Corona688
Such as someone editing a code solution in Microsoft Word, adding surplus linebreaks and converting ordinary quotes into special “ ” quotes. Smilie
I am glad to see that some people really pay attention to what is written.

Cheers. Smilie
# 11  
Old 04-19-2011
Well, they are explicit when one says CSV, but often neglected, so I did not drag anything new in. I am into robust solutions, but there may be no commas in the data here . . . yet.

There are JDBC and ODBC tools that can handle CSV files like database tables: filtering, combining and sorting them as you please to produce new CSV files. I used to correspond with a nice Chinese guy, Dawei, at HXTT, adding features to their flat file database tool. They have trial versions, and they work fine on the command line with xigole jisql. I also used JStels.

HXTT Text JDBC Drivers and CSV JDBC Drivers

Jisql - a Java based interactive SQL application

StelsCSV JDBC CSV Driver Documentation
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get duplicate rows from a csv file

How can i get the duplicates rows from a file using unix, for example i have data like a,1 b,2 c,3 d,4 a,1 c,3 e,5 i want output to be like a,1 c,3 (4 Replies)
Discussion started by: ggupta
4 Replies

2. Shell Programming and Scripting

Extract and exclude rows based on duplicate values

Hello I have a file like this: > cat examplefile ghi|NN603762|eee mno|NN607265|ttt pqr|NN613879|yyy stu|NN615002|uuu jkl|NN607265|rrr vwx|NN615002|iii yzA|NN618555|ooo def|NN190486|www BCD|NN628717|ppp abc|NN190486|qqq EFG|NN628717|aaa HIJ|NN628717|sss > I can sort the file by... (5 Replies)
Discussion started by: CHoggarth
5 Replies

3. Shell Programming and Scripting

Average values of duplicate rows

I have this file input.txt. I want to take average column-wise for the rows having duplicate gene names. Gene Sample_1 Sample_2 Sample_3 gene_A 2 4 5 gene_B 1 2 3 gene_A 0 5 7 gene_B 4 5 6 gene_A 11 12 13 gene_C 2 3 4 Desired output: gene_A 4.3 7 8.3 gene_B 2.5 3.5 4.5 gene_C 2 3 4... (6 Replies)
Discussion started by: Sanchari
6 Replies

4. Shell Programming and Scripting

Remove duplicate rows based on one column

Dear members, I need to filter a file based on the 8th column (that is id), and does not mather the other columns, because I want just one id (1 line of each id) and remove the duplicates lines based on this id (8th column), and does not matter wich duplicate will be removed. example of my file... (3 Replies)
Discussion started by: clarissab
3 Replies

5. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

6. Shell Programming and Scripting

printing 3 files side by side based on similar values in rows

Hi I'm trying to compare 3 or more files based on similar values and outputting them into 3 columns. For example: file1 ABC DEF GHI file2 DEF DER file3 ABC DER The output should come out like this file1 file2 file3 ABC ABC (4 Replies)
Discussion started by: zerofire123
4 Replies

7. UNIX for Dummies Questions & Answers

forming duplicate rows based on value of a key

if the key (A or B or ...others) has 4 in its 3rd column the 1st A row has to form 4 dupicates along with the all the values of A in 4th column (2.9, 3.8, 4.2) . Hope I explain the question clearly. Cheers Ruby input "A" 1 4 2.9 "A" 2 5 ... (7 Replies)
Discussion started by: ruby_sgp
7 Replies

8. Shell Programming and Scripting

how to delete duplicate rows based on last column

hii i have a huge amt of data stored in a file.Here in this file i need to remove duplicates rows in such a way that the last column has different data & i must check for greatest among last colmn data & print the largest data along with other entries but just one of other duplicate entries is... (16 Replies)
Discussion started by: reva
16 Replies

9. Shell Programming and Scripting

Duplicate rows in CSV files based on values

I want to duplicate a row if found two or more values in a particular column for corresponding row which is delimitted by comma. Input abc,line one,value1 abc,line two, value1, value2 abc,line three,value1 needs to converted to abc,line one,value1 abc,line two, value1 abc,line... (8 Replies)
Discussion started by: Incrediblian
8 Replies

10. UNIX for Dummies Questions & Answers

Remove duplicate rows of a file based on a value of a column

Hi, I am processing a file and would like to delete duplicate records as indicated by one of its column. e.g. COL1 COL2 COL3 A 1234 1234 B 3k32 2322 C Xk32 TTT A NEW XX22 B 3k32 ... (7 Replies)
Discussion started by: risk_sly
7 Replies
Login or Register to Ask a Question