Handling Comma in string values in a CSV file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Handling Comma in string values in a CSV file
# 8  
Old 08-11-2014
Thanks a lot but the above code is not working for below input:

Code:
"prabhat,kumar",19,2000,"bangalore,India","ABC,DEF"
"prabhat,kumar",19,2000,"bangalore,India","ABC,DEF"

Thank you Smilie

Last edited by prabhat.diwaker; 08-11-2014 at 02:14 PM..
# 9  
Old 08-12-2014
Hey Prabhat,

that above code was only made for the pattern you have given in previous threads. So let me check on this one and get back to you then.


Thanks,
R. Singh

---------- Post updated 08-12-14 at 02:14 AM ---------- Previous update was 08-11-14 at 02:00 PM ----------

Hello Prabhat,

The code which I have edited in previous post is the correct solution for same. Have a look at below for same and let me know if you have any queries.


Input file is as follows.
Code:
cat test
"prabhat,kumar",19,2000,"bangalore,India","ABC,DEF"
"prabhat,kumar",19,2000,"bangalore,India","ABC,DEF"


Code for same is as follows.

Code:
 awk -F"," '{for(i=1;i<=NF;i++){if(i%NF==0){ORS="\n"} {if($i ~ /^\"/ || $i ~ /\"$/) {a=a OFS $i;j++;{if(j%2==0){sub(/^\,/,X,a); print a;j=0;a=""}}} else {print $i}}} {ORS="|"}}' ORS="|" OFS=, test

Output will be as follows.

Code:
"prabhat,kumar"|19|2000|"bangalore,India"|"ABC,DEF"
"prabhat,kumar"|19|2000|"bangalore,India"|"ABC,DEF"


Thanks,
R. Singh
# 10  
Old 08-13-2014
Code:
awk -vq='"' '{c=split($0, a, q); out=X
  $0 = (a[1]) ? csv2del(1) : csv2del(2)}1
func csv2del(n) {
  for(i=n; i<=c; i++) {
    if(i%2 == 1) gsub(/,/, OFS, a[i])
    else a[i] = (q a[i] q)
    out = (out) ? out a[i] : a[i]}
  return out}' OFS='|' file

---------- Post updated 08-13-14 at 12:37 AM ---------- Previous update was 08-12-14 at 04:15 AM ----------

Another approach
Code:
awk -vq='"' '{n = split($0, a, q); gsub(/,/, OFS, a[1]); out = a[1];
  for(i=2; i<=n; i++) {if(i%2) gsub(/,/, OFS, a[i]); out = out q a[i]}
  print out}' OFS='|' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete a columns of a CSV file which has cell values with a string enclosed in " , "?

Hi How can I delete a columns from a CSV file which has comma separated value with a string enclosed in double quotes or square bracket and a comma in between? I have a csv file with below format. Template,Target Server,Target Component,Rule Group,Rule,Rule Reference Number,Rule... (7 Replies)
Discussion started by: Litu19
7 Replies

2. Shell Programming and Scripting

How to delete a column/columns of a CSV file which has cell values with a string enclosed in " , "?

How can I delete a column from a CSV file which has comma separated value with a string enclosed in double quotes and a comma in between? I have a file 44.csv with 4 lines including the header like the below format: column1, column2, column3, column 4, column5, column6 12,455,"string with... (6 Replies)
Discussion started by: dhruuv369
6 Replies

3. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

4. Shell Programming and Scripting

handling CSV file to get desired output

Hi All , i have a CSV file , pattern is given below :- Group # name # host # account # stop # # start # # check -------------------------------------------------------------------------- file format and data exmaple :- RBP2,RB0112,sihrb001,tksrb011,. ./.profile 1>/dev/null 2>&1;stop_olc_dmn... (0 Replies)
Discussion started by: deepakiniimt
0 Replies

5. UNIX for Advanced & Expert Users

Replacing the comma in .csv file in unix

Hi All, Could some one help me on one of my requirement below: I have a sequential file with 4fields in it and it is a comma (,) seperated file. Delimeter is 'comma'. But in of the file column for ex: 3rd column it is 'Description' (column name) I am getting the values with commas.... (6 Replies)
Discussion started by: eskay_s
6 Replies

6. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

7. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

8. Shell Programming and Scripting

Handling .CSV( Comma seperated value) in awk

Hi Guys, I am trying to reading in comma seperated values in awk. I can set the delimiter to be a comma, but the tricky part is that commas that appear within quotes are not to be considered as delimiters. Could someone please help. Regards, Laud (1 Reply)
Discussion started by: Laud12345
1 Replies

9. UNIX for Advanced & Expert Users

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (4 Replies)
Discussion started by: handynas
4 Replies

10. UNIX for Dummies Questions & Answers

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (5 Replies)
Discussion started by: handynas
5 Replies
Login or Register to Ask a Question