Comma seperator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comma seperator
# 1  
Old 02-15-2006
Comma seperator

I am trying to create a new CSV file from an existing CSV file whose content is as follows:

"1","Tom,Garry","111"
"2","Tom,Garry","222"

when i use Cat file | cut -d',' -f1,3 or awk to have only the 1st and 3rd column in my new CSV file, instead of creating a file content as
"1","111"
"2","222"

It is creating as
"1",Garry"
"2",Garry"

This is because of the comma in the second field.
Could you please help me how to sort it out.
# 2  
Old 02-15-2006
Try this

$cat file | cut -d'"' -f2,6 | tr -s '"' ','
1,111
2,222
# 3  
Old 02-15-2006
Or maybe...
Code:
awk 'BEGIN{OFS=FS="\",\""}{print $1,$3}' file

# 4  
Old 02-15-2006
Code:
 sed -e 's_\("[^"]*"\),.*,\("[^"]*"\)_\1,\2_g' input.txt

You broke the rules when you started this thread.

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post where your goal is to get an answer more quickly.

Your thread - Deleting specific columns from a file

Edit: Darn Smilie Ygor beat me !
# 5  
Old 02-15-2006
comma seperator

Hi Rahul I am very new to unix/shell scripting...I thank you for ur reply..
But my problem is :because of the comma in the second field "Tom,Garry" of my original file, the cut command when i cut based on comma splits this second field into two fields.

it is considering
"1" as 1st field
"Tom as 2nd filed
Garry" as 3rd field
"111" as 4th field

But i want
"1" as 1st field
"Tom,Garry" as 2nd filed
"111" as third field

so when i try to move 1st and 3rd column to a new file,it is moving the 1st and the second part of second column to the new file..
# 6  
Old 02-17-2006
comma seperator

Hi Guys sorry if i have violated the rules.Since my issue now has changed to a different one I created a new thread. Previously I had issue with cuting the columns in file and now my issue is about comma delimiter.
In some of other forums I have seen rules to create a new thread if our topic changes. Thats the reason I created a new thread.And this is my first thread.so I am unaware of the rules here..sorry..
# 7  
Old 02-17-2006
No Delimiter in Data

Premar,

The baisc idea of a delimiter is to have such a character as a field separator such that it does not appear in data. Thus while the file is created, it needs to be made sure that ',' will not be a part of the data or else you should use another delimiter.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Field seperator with awk

Hi, input data format: echo ' <APPLICATION="APPLSG" SUB_APPLICATION="DLY" JOBNAME="DPL_BN_RE_CCMS_SA" CMDLINE="run_job.ksh %%PARAM1 %%PARAM2" TASKTYPE="Command" />' expected format: "APPLSG", "DLY", "DPL_BN_RE_CCMS_SA", "run_job.ksh %%PARAM1 %%PARAM2" my command: echo ' ... (2 Replies)
Discussion started by: JSKOBS
2 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

4. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

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

6. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

7. Shell Programming and Scripting

Pull Data After Comma if 2 word before comma

Hi, I am trying to truncate word after comma in a file ONLY if there are already 2 words BEFORE comma. If there is one word or 3 or more words BEFORE comma, then I have to leave the data AS IS. See below for example. Input File : John Smith, Manager Smith, John Frank J F K... (2 Replies)
Discussion started by: msalam65
2 Replies

8. UNIX for Dummies Questions & Answers

Using | as a seperator in join

I need to use | as a seperator in unix join command. i tried changing seperator using -t option but iit is not working. can u please help. (5 Replies)
Discussion started by: firvin
5 Replies

9. Solaris

how i can use a WORD for seperator

hi, i want to use A WORD for seperator in awk or especially in cut. how i can perform this. is there any way to use a word for seperator. For example: i want to list all word after FROM and the files name contains this word. chatnorollback.svc: delete from info where nick ... (3 Replies)
Discussion started by: qrshat
3 Replies

10. Shell Programming and Scripting

Awk Field Seperator Help

I wrote a script on HPUX 11.11 to turn a Decimal subnet mask (255.255.254.0) to hex 0xfffffe00 (subset of a bigger script). It works great on the HPUX systems but on the freebsd box the awk is not seperating the fields properly. I tried to google for a solution and seaching these forums i am just... (3 Replies)
Discussion started by: insania
3 Replies
Login or Register to Ask a Question