SED not removing last double quote from every line
up vote
0
down vote
favorite
I have a CSV that was exported with double quotes around every header and value and I need them gone but without removing double quotes that might actually exist as values. For example:
"HEADER1","HEADER2","HEADER3","HEADER4","HEADER5"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
I can remove the first "
from every line with the following
$ sed -i.bak 's/^"//g' $1
And I can remove all the ones in between with this:
$ sed -i.bak 's/","/,/g' $1
And finally I thought I could remove the last "
in every line with this:
$ sed -i.bak 's/"$//g' $1
But it's not working. Could I accomplish this entire task in one line?
UPDATE
I used this website to paste my data for hidden characters and here was the results
It appears some of the comment may be accurate but I don't know what that means I need to change still. Also is there a a clean way to check if the CSV has these quotes before attempting to remove them? Perhaps even just qualifying that the first character is a quote?
sed csv csv-simple
New contributor
add a comment |
up vote
0
down vote
favorite
I have a CSV that was exported with double quotes around every header and value and I need them gone but without removing double quotes that might actually exist as values. For example:
"HEADER1","HEADER2","HEADER3","HEADER4","HEADER5"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
I can remove the first "
from every line with the following
$ sed -i.bak 's/^"//g' $1
And I can remove all the ones in between with this:
$ sed -i.bak 's/","/,/g' $1
And finally I thought I could remove the last "
in every line with this:
$ sed -i.bak 's/"$//g' $1
But it's not working. Could I accomplish this entire task in one line?
UPDATE
I used this website to paste my data for hidden characters and here was the results
It appears some of the comment may be accurate but I don't know what that means I need to change still. Also is there a a clean way to check if the CSV has these quotes before attempting to remove them? Perhaps even just qualifying that the first character is a quote?
sed csv csv-simple
New contributor
5
Perhaps"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?
– steeldriver
Dec 6 at 0:29
1
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
have you trieddos2unix
??
– msp9011
Dec 6 at 6:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a CSV that was exported with double quotes around every header and value and I need them gone but without removing double quotes that might actually exist as values. For example:
"HEADER1","HEADER2","HEADER3","HEADER4","HEADER5"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
I can remove the first "
from every line with the following
$ sed -i.bak 's/^"//g' $1
And I can remove all the ones in between with this:
$ sed -i.bak 's/","/,/g' $1
And finally I thought I could remove the last "
in every line with this:
$ sed -i.bak 's/"$//g' $1
But it's not working. Could I accomplish this entire task in one line?
UPDATE
I used this website to paste my data for hidden characters and here was the results
It appears some of the comment may be accurate but I don't know what that means I need to change still. Also is there a a clean way to check if the CSV has these quotes before attempting to remove them? Perhaps even just qualifying that the first character is a quote?
sed csv csv-simple
New contributor
I have a CSV that was exported with double quotes around every header and value and I need them gone but without removing double quotes that might actually exist as values. For example:
"HEADER1","HEADER2","HEADER3","HEADER4","HEADER5"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
"SOME_ID_0X0","SOME_ID_1X2","false","Some blob value with "double quotes" inside of it"
I can remove the first "
from every line with the following
$ sed -i.bak 's/^"//g' $1
And I can remove all the ones in between with this:
$ sed -i.bak 's/","/,/g' $1
And finally I thought I could remove the last "
in every line with this:
$ sed -i.bak 's/"$//g' $1
But it's not working. Could I accomplish this entire task in one line?
UPDATE
I used this website to paste my data for hidden characters and here was the results
It appears some of the comment may be accurate but I don't know what that means I need to change still. Also is there a a clean way to check if the CSV has these quotes before attempting to remove them? Perhaps even just qualifying that the first character is a quote?
sed csv csv-simple
sed csv csv-simple
New contributor
New contributor
edited yesterday
Rui F Ribeiro
38.4k1479128
38.4k1479128
New contributor
asked Dec 6 at 0:20
Xtremefaith
1032
1032
New contributor
New contributor
5
Perhaps"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?
– steeldriver
Dec 6 at 0:29
1
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
have you trieddos2unix
??
– msp9011
Dec 6 at 6:47
add a comment |
5
Perhaps"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?
– steeldriver
Dec 6 at 0:29
1
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
have you trieddos2unix
??
– msp9011
Dec 6 at 6:47
5
5
Perhaps
"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?– steeldriver
Dec 6 at 0:29
Perhaps
"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?– steeldriver
Dec 6 at 0:29
1
1
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
have you tried
dos2unix
??– msp9011
Dec 6 at 6:47
have you tried
dos2unix
??– msp9011
Dec 6 at 6:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Use dos2unix
to convert DOS to UNIX text file format
dos2unix $1
You could combine all 3 sed
as:
sed -i 's/^"//g;s/","/,/g;s/"$//g' $1
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
Using AWK
:
awk -F ',' -v OFS=',' '{for (i=1;i<=NF;i++) sub(/^"/,"",$i) sub (/"$/,"",$i); print $0}' 1$
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
sub(/^"/,"",$i)
removes the"
at start of each field.
sub(/"$/,"",$i)
removes the"
at end of each field.
1
You can do "dos2unix" in sed:sed 's/r$//' file
, so removing trailing quote issed 's/r$//; s/"$//'
-- should work even if file has unix format.
– glenn jackman
2 days ago
add a comment |
up vote
0
down vote
Running 'sed -i.bak' 3 times will overwrite the backup file each time, so at the end you won't have the original file in your backup file. You should use '-i.bak' once and '-i' the rest of the times.
sed -i.bak -e 's/^"//' -e 's/","/,/g' -e 's/"$//' a.txt
Use -e <expression>
to use multiple sed expressions.
You don't need the 'g' for the first and last lines, because you're only doing one replacement.
I don't know why the last one isn't working for you, because it's correct as written. Either a typo on your end or maybe an issue with an environment variable or locale.
Another way to do it is search for
- a quote
- text that isn't a quote
- a quote
- either a comma or end-of-line
and keep and restore the 2nd and 4th parts. Like this:
sed 's/"([^"]*)"([,$])/12/g' a.txt
The (
and )
store the 2nd and 4th parts, and they're restored in the replace portion.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Use dos2unix
to convert DOS to UNIX text file format
dos2unix $1
You could combine all 3 sed
as:
sed -i 's/^"//g;s/","/,/g;s/"$//g' $1
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
Using AWK
:
awk -F ',' -v OFS=',' '{for (i=1;i<=NF;i++) sub(/^"/,"",$i) sub (/"$/,"",$i); print $0}' 1$
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
sub(/^"/,"",$i)
removes the"
at start of each field.
sub(/"$/,"",$i)
removes the"
at end of each field.
1
You can do "dos2unix" in sed:sed 's/r$//' file
, so removing trailing quote issed 's/r$//; s/"$//'
-- should work even if file has unix format.
– glenn jackman
2 days ago
add a comment |
up vote
2
down vote
accepted
Use dos2unix
to convert DOS to UNIX text file format
dos2unix $1
You could combine all 3 sed
as:
sed -i 's/^"//g;s/","/,/g;s/"$//g' $1
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
Using AWK
:
awk -F ',' -v OFS=',' '{for (i=1;i<=NF;i++) sub(/^"/,"",$i) sub (/"$/,"",$i); print $0}' 1$
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
sub(/^"/,"",$i)
removes the"
at start of each field.
sub(/"$/,"",$i)
removes the"
at end of each field.
1
You can do "dos2unix" in sed:sed 's/r$//' file
, so removing trailing quote issed 's/r$//; s/"$//'
-- should work even if file has unix format.
– glenn jackman
2 days ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Use dos2unix
to convert DOS to UNIX text file format
dos2unix $1
You could combine all 3 sed
as:
sed -i 's/^"//g;s/","/,/g;s/"$//g' $1
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
Using AWK
:
awk -F ',' -v OFS=',' '{for (i=1;i<=NF;i++) sub(/^"/,"",$i) sub (/"$/,"",$i); print $0}' 1$
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
sub(/^"/,"",$i)
removes the"
at start of each field.
sub(/"$/,"",$i)
removes the"
at end of each field.
Use dos2unix
to convert DOS to UNIX text file format
dos2unix $1
You could combine all 3 sed
as:
sed -i 's/^"//g;s/","/,/g;s/"$//g' $1
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
Using AWK
:
awk -F ',' -v OFS=',' '{for (i=1;i<=NF;i++) sub(/^"/,"",$i) sub (/"$/,"",$i); print $0}' 1$
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
SOME_ID_0X0,SOME_ID_1X2,false,Some blob value with "double quotes" inside of it
sub(/^"/,"",$i)
removes the"
at start of each field.
sub(/"$/,"",$i)
removes the"
at end of each field.
edited Dec 6 at 6:52
answered Dec 6 at 5:23
msp9011
3,64043863
3,64043863
1
You can do "dos2unix" in sed:sed 's/r$//' file
, so removing trailing quote issed 's/r$//; s/"$//'
-- should work even if file has unix format.
– glenn jackman
2 days ago
add a comment |
1
You can do "dos2unix" in sed:sed 's/r$//' file
, so removing trailing quote issed 's/r$//; s/"$//'
-- should work even if file has unix format.
– glenn jackman
2 days ago
1
1
You can do "dos2unix" in sed:
sed 's/r$//' file
, so removing trailing quote is sed 's/r$//; s/"$//'
-- should work even if file has unix format.– glenn jackman
2 days ago
You can do "dos2unix" in sed:
sed 's/r$//' file
, so removing trailing quote is sed 's/r$//; s/"$//'
-- should work even if file has unix format.– glenn jackman
2 days ago
add a comment |
up vote
0
down vote
Running 'sed -i.bak' 3 times will overwrite the backup file each time, so at the end you won't have the original file in your backup file. You should use '-i.bak' once and '-i' the rest of the times.
sed -i.bak -e 's/^"//' -e 's/","/,/g' -e 's/"$//' a.txt
Use -e <expression>
to use multiple sed expressions.
You don't need the 'g' for the first and last lines, because you're only doing one replacement.
I don't know why the last one isn't working for you, because it's correct as written. Either a typo on your end or maybe an issue with an environment variable or locale.
Another way to do it is search for
- a quote
- text that isn't a quote
- a quote
- either a comma or end-of-line
and keep and restore the 2nd and 4th parts. Like this:
sed 's/"([^"]*)"([,$])/12/g' a.txt
The (
and )
store the 2nd and 4th parts, and they're restored in the replace portion.
add a comment |
up vote
0
down vote
Running 'sed -i.bak' 3 times will overwrite the backup file each time, so at the end you won't have the original file in your backup file. You should use '-i.bak' once and '-i' the rest of the times.
sed -i.bak -e 's/^"//' -e 's/","/,/g' -e 's/"$//' a.txt
Use -e <expression>
to use multiple sed expressions.
You don't need the 'g' for the first and last lines, because you're only doing one replacement.
I don't know why the last one isn't working for you, because it's correct as written. Either a typo on your end or maybe an issue with an environment variable or locale.
Another way to do it is search for
- a quote
- text that isn't a quote
- a quote
- either a comma or end-of-line
and keep and restore the 2nd and 4th parts. Like this:
sed 's/"([^"]*)"([,$])/12/g' a.txt
The (
and )
store the 2nd and 4th parts, and they're restored in the replace portion.
add a comment |
up vote
0
down vote
up vote
0
down vote
Running 'sed -i.bak' 3 times will overwrite the backup file each time, so at the end you won't have the original file in your backup file. You should use '-i.bak' once and '-i' the rest of the times.
sed -i.bak -e 's/^"//' -e 's/","/,/g' -e 's/"$//' a.txt
Use -e <expression>
to use multiple sed expressions.
You don't need the 'g' for the first and last lines, because you're only doing one replacement.
I don't know why the last one isn't working for you, because it's correct as written. Either a typo on your end or maybe an issue with an environment variable or locale.
Another way to do it is search for
- a quote
- text that isn't a quote
- a quote
- either a comma or end-of-line
and keep and restore the 2nd and 4th parts. Like this:
sed 's/"([^"]*)"([,$])/12/g' a.txt
The (
and )
store the 2nd and 4th parts, and they're restored in the replace portion.
Running 'sed -i.bak' 3 times will overwrite the backup file each time, so at the end you won't have the original file in your backup file. You should use '-i.bak' once and '-i' the rest of the times.
sed -i.bak -e 's/^"//' -e 's/","/,/g' -e 's/"$//' a.txt
Use -e <expression>
to use multiple sed expressions.
You don't need the 'g' for the first and last lines, because you're only doing one replacement.
I don't know why the last one isn't working for you, because it's correct as written. Either a typo on your end or maybe an issue with an environment variable or locale.
Another way to do it is search for
- a quote
- text that isn't a quote
- a quote
- either a comma or end-of-line
and keep and restore the 2nd and 4th parts. Like this:
sed 's/"([^"]*)"([,$])/12/g' a.txt
The (
and )
store the 2nd and 4th parts, and they're restored in the replace portion.
edited Dec 6 at 0:33
answered Dec 6 at 0:26
drewbenn
5,14251836
5,14251836
add a comment |
add a comment |
Xtremefaith is a new contributor. Be nice, and check out our Code of Conduct.
Xtremefaith is a new contributor. Be nice, and check out our Code of Conduct.
Xtremefaith is a new contributor. Be nice, and check out our Code of Conduct.
Xtremefaith is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486259%2fsed-not-removing-last-double-quote-from-every-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Perhaps
"
is not really the last character on each line (either because of trailing whitespace, or even because of DOS CRLF line endings)?– steeldriver
Dec 6 at 0:29
1
That actually works fine for me when I copy & paste your data into a file; I'll hazard a guess that your real data differs from what you showed us, and that there's either a space or some other non-printing character in the last position before the n ..
– tink
Dec 6 at 0:30
have you tried
dos2unix
??– msp9011
Dec 6 at 6:47