Replace comma which is not inside brackets,quotes or paranthesis


 
Thread Tools Search this Thread
Top Forums Programming Replace comma which is not inside brackets,quotes or paranthesis
# 1  
Old 02-18-2015
Replace comma which is not inside brackets,quotes or paranthesis

Hi All,

I want to replace the commas which are not inside parenthesis,quotes

if input is
Code:
 abc,[def,ghi,ijk],lm,(no,pq,rs),{tu,vw,xy},zs,"as,as,fr",'ab,cd,ef'

output should be
Code:
 abc [def,ghi,ijk] lm (no,pq,rs) {tu,vw,xy} zs "as,as,fr" 'ab,cd,ef'

I tried this str.replaceAll("\\(.*?\\)|(,)", " "); say my string is str, it will replace commas which are not inside ( ) but output will not have content inside ( )

Any help is highly appreciated

Thanks,
Pre

Last edited by preethy; 02-18-2015 at 02:45 AM..
# 2  
Old 02-18-2015
Hello Pre,

If input is exactly in same format in which you showed then following may help.
Code:
awk '{gsub(/,\[/," [",$0);gsub(/],/,"] ",$0);gsub(/,\(/," (",$0);gsub(/\), /,")",$0);gsub(/,\"/," \"",$0);gsub(/\",/,"\" ",$0);gsub(/,{/," {",$0);gsub(/},/,"} ",$0);;print}'   Input_file

Output will be as follows.
Code:
abc [def,ghi,ijk] lm (no,pq,rs) {tu,vw,xy} zs "as,as,fr" 'ab,cd,ef'

Thanks,
R. Singh
# 3  
Old 02-18-2015
It looks like javascript question if my guess is true then try

Code:
<script>
var str="abc,[def,ghi,ijk],lm,(no,pq,rs),{tu,vw,xy},zs,\"as,as,fr\",'ab,cd,ef'";
console.log(str);
res = str.replace(/,(?=(?:"[^"]*"|\047[^\047]*\047|\([^()]*\)|\[[^\[\]]*\]|\{[^{}]*}|[^"\047\[{}()\]])*$)/g, " ");
console.log(res);
</script>

Edit fiddle - JSFiddle

else try this

Code:
[akshay@localhost tmp]$ cat infile
abc,[def,ghi,ijk],lm,(no,pq,rs),{tu,vw,xy},zs,"as,as,fr",'ab,cd,ef'

[akshay@localhost tmp]$ perl -pe 's/,(?=(?:"[^"]*"|\047[^\047]*\047|\([^()]*\)|\[[^\[\]]*\]|\{[^{}]*}|[^"\047\[{}()\]])*$)/ /g' infile
abc [def,ghi,ijk] lm (no,pq,rs) {tu,vw,xy} zs "as,as,fr" 'ab,cd,ef'

# 4  
Old 02-18-2015
Try
Code:
awk     '       {for (i=1; i<=NF; i++)
                        {if ($i~/[[({]/) IN=1
                         if ($i~/[])}]/) IN=0
                         if ($i~/[\"\047]/)  IN=!IN
                         if (!IN && ($i==",")) $i=" "}
                        }
         1
        ' FS="" OFS="" file
abc [def,ghi,ijk] lm (no,pq,rs) {tu,vw,xy} zs "as,as,fr" 'ab,cd,ef' kl jm ke

---------- Post updated at 11:06 ---------- Previous update was at 11:05 ----------

or even
Code:
awk     '       {for (i=1; i<=NF; i++)
                        {if ($i~/[])}[({\"\047]/)  IN=!IN
                         if (!IN && ($i==",")) $i=" "}
                        }
         1
        ' FS="" OFS="" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies

2. Shell Programming and Scripting

Replace double quotes inside the string data for all the columns

Please use code tags Hi, I have input data is below format and n of column in the multiple flat files. the string data has any double quotes(") values replaced to double double quotes for all the columns{""). Also, my input flat file each column string data has carriage of new line too.... (14 Replies)
Discussion started by: SSrini
14 Replies

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

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

Remove everything inside of brackets

I need to use something bash related to remove everything inside of brackets. For example. In the following: abc<def>ghi<jkl>mno the result should be: abcghimno (4 Replies)
Discussion started by: locoroco
4 Replies

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

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. Shell Programming and Scripting

To Replace comma with Pipe inside double quotes

Hi, I have a requirement to replace the comma's inside the double quotes. The comma's inside the double quotes will get changed dynamically. Input Record: "Washington, DC,Prabhu,aju",New York Output Record: "Washington| DC|Prabhu|aju",New York I tried with the below command but it... (3 Replies)
Discussion started by: prabhutkl
3 Replies

9. Web Development

PHP echo everything including brackets and quotes

How can I print exactly what I want in between my quotes? I have xml that I want to echo exactly and it is not printing out the brackets and gets hung up at the "<?" and all that. Is there a way I can just say echo exactly between these quotes? (2 Replies)
Discussion started by: mainegate
2 Replies

10. Shell Programming and Scripting

sed removing comma inside double quotes

I have a csv file with lines like the followings 123456,"ABC CO., LTD","XXX" 789012,"DEF LIMITED", "XXX" before I bcp this file to database, the comma in "CO.," need to be removed first. My script is cat <filename> | sed 's/"CO.,"/"CO."/g' but it doesn't work. Can anyone here able to... (2 Replies)
Discussion started by: joanneho
2 Replies
Login or Register to Ask a Question