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
# 1  
Old 04-24-2009
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

Code:
abc,line one,value1
abc,line two, value1, value2
abc,line three,value1

needs to converted to

Code:
abc,line one,value1
abc,line two, value1
abc,line two, value2
abc,line three,value1

How this could be done using unix script???

Thanks in advance..........

Last edited by Yogesh Sawant; 04-24-2009 at 10:11 AM.. Reason: added code tags
# 2  
Old 04-24-2009
hello Incrediblian,
one thing I want to confirm is
"abc,line" will be constant or it may change.
# 3  
Old 04-24-2009
Duplicate rows in CSV files based on values

Hi pradeep,

Thanks for your reply, No its not same, all the field would to be unique.
Input

abc, first line, value1
def, second line, value2,value3
ghi, third line, value4

need to be

abc, first line, value1
def, second line, value2
def, second line, value3
ghi, third line, value4


Thanks in advance....
# 4  
Old 04-24-2009
Code:
perl -F, -lane'
if ( @F > 3 ) {
    print join ",", @F[ 0, 1, $_ ] for 2 .. @F - 1;
}
else {
    print;
}' infile


Last edited by radoulov; 04-24-2009 at 11:11 AM.. Reason: refactored
# 5  
Old 04-24-2009
Duplicate rows in CSV files based on values

hi radoulov,

Thank you so much,

I am new to perl and unix as well. can you please explain me ?

Cant this be done using unix ?

Thanks in advance.
# 6  
Old 04-25-2009
script is as below:
#! /bin/bash
while read line
do
first=`echo $line | awk -F "line" '{print $1}'
first=`echo $first line,`
value=`echo $line | awk -F "line" '{print $2}' | sed 's/,/ /g'`
for i in `echo $value`
do
echo $first $i
done
done < ref_file



Contents of ref_file is as below
abc, first line, value1
def, second line, value2,value3, value4
ghi, third line, value4 , value5
# 7  
Old 04-25-2009
hello Incrediblian ,

This will work very well compared than previous one.

while read line
do
first=`echo $line | awk -F "," '{print $1}'`
second=`echo $line | awk -F "," '{print $2}'`
first_half=`echo $first,$second, `
value=`echo $line | cut -d "," -f 3- | sed 's/,/ /g'`
for i in `echo $value`
do
echo $first_half$i
done
done < ref


Contents of ref_file is as below:
abc,line first,value1
def,line second,value2,value3, value4
ghi,line third,value4, value2
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

Duplicate rows in CSV files based on values

I am new to this forum and this is my first post. I am looking at an old post with exactly the same name. Can not paste URL because I do not have 5 posts My requirement is exactly opposite. I want to get rid of duplicate rows and try to append the values of columns in those rows ... (10 Replies)
Discussion started by: vbhonde11
10 Replies

7. 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

8. 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

9. 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

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