Remove comma outside quotes
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I have a input file delimited with commas (,
). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row
123,"ABC, DEV 23",345,534.202,NAME
I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:
123~"ABC, DEV 23"~345~534.202~NAME
I have tried this, but it gives me reverse output:
awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME
text-processing awk replace
add a comment |Â
up vote
3
down vote
favorite
I have a input file delimited with commas (,
). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row
123,"ABC, DEV 23",345,534.202,NAME
I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:
123~"ABC, DEV 23"~345~534.202~NAME
I have tried this, but it gives me reverse output:
awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME
text-processing awk replace
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a input file delimited with commas (,
). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row
123,"ABC, DEV 23",345,534.202,NAME
I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:
123~"ABC, DEV 23"~345~534.202~NAME
I have tried this, but it gives me reverse output:
awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME
text-processing awk replace
I have a input file delimited with commas (,
). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row
123,"ABC, DEV 23",345,534.202,NAME
I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:
123~"ABC, DEV 23"~345~534.202~NAME
I have tried this, but it gives me reverse output:
awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME
text-processing awk replace
edited Feb 7 at 17:09
ñÃÂsýù÷
14.9k82462
14.9k82462
asked Feb 7 at 12:34
Sudeep Adhya
162
162
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45
add a comment |Â
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
5
down vote
You basically have a CSV file that you would like to replace the delimiter in, from ,
to ~
.
Using csvkit
:
$ csvformat -D '~' file.csv >newfile.csv
$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME
cvsformat
removes the quotation marks that are not needed. To add quotation marks:
$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"
See csvformat --help
for usage info.
add a comment |Â
up vote
2
down vote
GNU awk
solution:
awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file
FPAT='[^,]+|"[^"]+"'
- regex pattern describing each field is either âÂÂanything that is not a comma,â or âÂÂa double quote, anything that is not a double quote, and a closing double quote.âÂÂ
The output:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
0
down vote
I have done by below 2 methods for above example . Tested
Method1
for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'
output
123~"ABC, DEV 23"~345~534.202~NAME'
Method2
sed "s/,/~/3g" inputfile| sed 's/,/~/1'
Output
123~"ABC, DEV 23"~345~534.202~NAME'
add a comment |Â
up vote
0
down vote
You can try this awk
awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile
add a comment |Â
up vote
0
down vote
In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:
ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '
Examples:
1,"2,3"
results in
1~2,3
and
1,"
2,3
",4
results in
1~"
2,3
"~4
add a comment |Â
up vote
0
down vote
This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.
awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile
Output is:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
0
down vote
awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file
output
123~"ABC, DEV 23"~345~534.202~NAME
The very first comma is replaced by sub and the rest by gsub in third field.
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You basically have a CSV file that you would like to replace the delimiter in, from ,
to ~
.
Using csvkit
:
$ csvformat -D '~' file.csv >newfile.csv
$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME
cvsformat
removes the quotation marks that are not needed. To add quotation marks:
$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"
See csvformat --help
for usage info.
add a comment |Â
up vote
5
down vote
You basically have a CSV file that you would like to replace the delimiter in, from ,
to ~
.
Using csvkit
:
$ csvformat -D '~' file.csv >newfile.csv
$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME
cvsformat
removes the quotation marks that are not needed. To add quotation marks:
$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"
See csvformat --help
for usage info.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You basically have a CSV file that you would like to replace the delimiter in, from ,
to ~
.
Using csvkit
:
$ csvformat -D '~' file.csv >newfile.csv
$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME
cvsformat
removes the quotation marks that are not needed. To add quotation marks:
$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"
See csvformat --help
for usage info.
You basically have a CSV file that you would like to replace the delimiter in, from ,
to ~
.
Using csvkit
:
$ csvformat -D '~' file.csv >newfile.csv
$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME
cvsformat
removes the quotation marks that are not needed. To add quotation marks:
$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"
See csvformat --help
for usage info.
edited Feb 23 at 12:21
answered Feb 7 at 12:52
Kusalananda
103k13202318
103k13202318
add a comment |Â
add a comment |Â
up vote
2
down vote
GNU awk
solution:
awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file
FPAT='[^,]+|"[^"]+"'
- regex pattern describing each field is either âÂÂanything that is not a comma,â or âÂÂa double quote, anything that is not a double quote, and a closing double quote.âÂÂ
The output:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
2
down vote
GNU awk
solution:
awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file
FPAT='[^,]+|"[^"]+"'
- regex pattern describing each field is either âÂÂanything that is not a comma,â or âÂÂa double quote, anything that is not a double quote, and a closing double quote.âÂÂ
The output:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
2
down vote
up vote
2
down vote
GNU awk
solution:
awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file
FPAT='[^,]+|"[^"]+"'
- regex pattern describing each field is either âÂÂanything that is not a comma,â or âÂÂa double quote, anything that is not a double quote, and a closing double quote.âÂÂ
The output:
123~"ABC, DEV 23"~345~534.202~NAME
GNU awk
solution:
awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file
FPAT='[^,]+|"[^"]+"'
- regex pattern describing each field is either âÂÂanything that is not a comma,â or âÂÂa double quote, anything that is not a double quote, and a closing double quote.âÂÂ
The output:
123~"ABC, DEV 23"~345~534.202~NAME
edited Feb 7 at 13:09
answered Feb 7 at 13:03
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
0
down vote
I have done by below 2 methods for above example . Tested
Method1
for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'
output
123~"ABC, DEV 23"~345~534.202~NAME'
Method2
sed "s/,/~/3g" inputfile| sed 's/,/~/1'
Output
123~"ABC, DEV 23"~345~534.202~NAME'
add a comment |Â
up vote
0
down vote
I have done by below 2 methods for above example . Tested
Method1
for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'
output
123~"ABC, DEV 23"~345~534.202~NAME'
Method2
sed "s/,/~/3g" inputfile| sed 's/,/~/1'
Output
123~"ABC, DEV 23"~345~534.202~NAME'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I have done by below 2 methods for above example . Tested
Method1
for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'
output
123~"ABC, DEV 23"~345~534.202~NAME'
Method2
sed "s/,/~/3g" inputfile| sed 's/,/~/1'
Output
123~"ABC, DEV 23"~345~534.202~NAME'
I have done by below 2 methods for above example . Tested
Method1
for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'
output
123~"ABC, DEV 23"~345~534.202~NAME'
Method2
sed "s/,/~/3g" inputfile| sed 's/,/~/1'
Output
123~"ABC, DEV 23"~345~534.202~NAME'
answered Feb 7 at 16:46
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
up vote
0
down vote
You can try this awk
awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile
add a comment |Â
up vote
0
down vote
You can try this awk
awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can try this awk
awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile
You can try this awk
awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile
answered Feb 7 at 20:54
ctac_
1,016116
1,016116
add a comment |Â
add a comment |Â
up vote
0
down vote
In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:
ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '
Examples:
1,"2,3"
results in
1~2,3
and
1,"
2,3
",4
results in
1~"
2,3
"~4
add a comment |Â
up vote
0
down vote
In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:
ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '
Examples:
1,"2,3"
results in
1~2,3
and
1,"
2,3
",4
results in
1~"
2,3
"~4
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:
ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '
Examples:
1,"2,3"
results in
1~2,3
and
1,"
2,3
",4
results in
1~"
2,3
"~4
In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:
ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '
Examples:
1,"2,3"
results in
1~2,3
and
1,"
2,3
",4
results in
1~"
2,3
"~4
answered Feb 8 at 17:45
JoL
68819
68819
add a comment |Â
add a comment |Â
up vote
0
down vote
This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.
awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile
Output is:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
0
down vote
This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.
awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile
Output is:
123~"ABC, DEV 23"~345~534.202~NAME
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.
awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile
Output is:
123~"ABC, DEV 23"~345~534.202~NAME
This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.
awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile
Output is:
123~"ABC, DEV 23"~345~534.202~NAME
edited Feb 8 at 18:02
answered Feb 7 at 17:09
ñÃÂsýù÷
14.9k82462
14.9k82462
add a comment |Â
add a comment |Â
up vote
0
down vote
awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file
output
123~"ABC, DEV 23"~345~534.202~NAME
The very first comma is replaced by sub and the rest by gsub in third field.
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
add a comment |Â
up vote
0
down vote
awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file
output
123~"ABC, DEV 23"~345~534.202~NAME
The very first comma is replaced by sub and the rest by gsub in third field.
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
add a comment |Â
up vote
0
down vote
up vote
0
down vote
awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file
output
123~"ABC, DEV 23"~345~534.202~NAME
The very first comma is replaced by sub and the rest by gsub in third field.
awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file
output
123~"ABC, DEV 23"~345~534.202~NAME
The very first comma is replaced by sub and the rest by gsub in third field.
edited Feb 23 at 12:18
answered Feb 23 at 3:14
Claes Wikner
11713
11713
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
add a comment |Â
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
The code may provide answer to the question, but any explanations will be highly appretiated
â Romeo Ninov
Feb 23 at 7:40
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422526%2fremove-comma-outside-quotes%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
â Kiwy
Feb 7 at 12:45