I try to use sed within a bash script in order to replace ' with ''
I have to do so because of an Oracle DB, but I cannot find the right syntax.
So far I have tried:
sed -e 's/(')/('')/g' List_employees_a.csv > List_employees.csv
sed -e 's/'/''/g' List_employees_a.csv > List_employees.csv
sed -e 's/'/\'\'/g' List_employees_a.csv > List_employees.csv
sed -e 's/\'/\''/g' List_employees_a.csv > List_employees.csv
This sed should work:
sed 's/'\''/'\'''\''/g'
OR use double quoted for delimiters:
sed "s/'/''/g"
OR more verbose:
sq="'"
dq="''"
sed "s/$sq/$dq/g" file
The problem isn't sed, it's that your shell is parsing the quotes before they ever get to sed. One really easy way to avoid this is to use a script file:
sed -f script_file List_employees_a.csv > List_employees.csv
where the content of script_file is:
s/'/''/g
What about this one?
sed "s/'/''/g"
Test
$ cat file
hello'my name is
quote' and double quote" blabla
$ sed "s/'/''/g" file
hello''my name is
quote'' and double quote" blabla
In the single-quoted string notation in sh, the only special character is the single-quote ' itself -- even backslash \ is not special. Juxtaposition is concatenation, so you can string together multiple string notations: 'foo''bar' is the same as "foobar", and 'foo'\''bar' and 'foo'"'"'bar' are both the same as "foo'bar".
You don't have to use single-quoted notation:
sed -e "s/'/\"/g"
But if you really want to, you can write
sed -e 's/'\''/"/g'
or
sed -e 's/'"'"'/"/g'
I am not a sed expert so I am not sure what you try to achieve with (') etc. And you write in your comment that you need to replace ' with ", so why to you write ' ' in your attempts?
Anyway, I did some testing and a normal escape of the quotes did it for me. Hope it helps.
sed -e s/\'/\'\'/g List_employees_a.csv > List_employees.csv
Related
I would like to change a string with a string variable. The string is made out of '"+a+"' (this is exactly the string, no extra quotes were added. For example having the string "foo" and the string foo my example is the second foo)
Is it possible to change that using the sed command?
I want to replace it with a variable. I have tried this:
sed "s#'"+a+"'#${var}#"
sed "s#'\"+a+\"'#${var}#"
sed "s#'[\"]+a+[\"]'#${var}#"
Thanks for any answers!
You have a typo in your sed expressions - you're missing the second + symbol after a :)
$ var=" else "
$ echo "something'\"+a+\"'cool" | sed "s#'\"+a+\"'#${var}#"
something else cool
Perhaps use a variable also for the search.
var=_FOO_
search=$'\'"+a+"\''
echo "mama${search}papa" | sed "s#${search}#${var}#"
Output
mama_FOO_papa
The $' ' is bash style quoting.
search=$'\'"+a+"\''; echo "$search"
Output
'"+a+"'
It's possible using BSD sed (e.g. macOS): sed "s/\'\"\+\a\+\"\'/$var/g"
And this is ugly but it works (GNU sed): sed "s/[\'][\"][\+]a[\+][\"][\']/$var/g"
I have abc.sh:
exec $ROOT/Subsystem/xyz.sh
On a Unix box, if I print echo $HOME then I get /HOME/COM/FILE.
I want to replace $ROOT with $HOME using sed.
Expected Output:
exec /HOME/COM/FILE/Subsystem/xyz.sh
I tried, but I'm not getting the expected output:
sed 's/$ROOT/"${HOME}"/g' abc.sh > abc.sh.1
Addition:
If I have abc.sh
exec $ROOT/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
then with
sed "s|\$INSTALLROOT/|${INSTALLROOT}|" abc.sh
it is only replacing first $ROOT, i.e., output is coming as
exec /HOME/COM/FILE/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
Say:
sed "s|\$ROOT|${HOME}|" abc.sh
Note:
Use double quotes so that the shell would expand variables.
Use a separator different than / since the replacement contains /
Escape the $ in the pattern since you don't want to expand it.
EDIT: In order to replace all occurrences of $ROOT, say
sed "s|\$ROOT|${HOME}|g" abc.sh
This might work for you:
sed 's|$ROOT|'"${HOME}"'|g' abc.sh > abc.sh.1
This may also can help
input="inputtext"
output="outputtext"
sed "s/$input/${output}/" inputfile > outputfile
The safe for a special chars workaround from https://www.baeldung.com/linux/sed-substitution-variables with improvement for \ char:
#!/bin/bash
to="/foo\\bar#baz"
echo "str %FROM% str" | sed "s#%FROM%#$(echo ${to//\\/\\\\} | sed 's/#/\\#/g')#g"
Using bash, how would one replace all unquoted characters from a file?
I have a system that I can't modify that spits out CSV files such as:
code;prop1;prop2;prop3;prop4;prop5;prop6
0,1000,89,"a1,a2,a3",33,,
1,,,"a55,a10",1,1 L,87
2,25,1001,a4,,"1,5 L",
I need this to become, for a new system being added
code;prop1;prop2;prop3;prop4;prop5;prop6
0;1000;89;a1,a2,a3;33;;
1;;;a55,a10;1;1 L;87
2;25;1001;a4;1,5 L;
If the quotes can be removed after this substitution happens in one command it would be nice :) But I prefer clarity to complicated one-liners for future maintenance.
Thank you
With sed:
sed -e 's/,/;/g' -e ':loop; s/\("\)\([^;]*\);\([^"]*"\)/\1\2,\3/; t loop'
Test:
$ sed -e 's/,/;/g' -e ':loop; s/\("\)\([^;]*\);\([^"]*"\)/\1\2,\3/; t loop' yourfile
code;prop1;prop2;prop3;prop4;prop5;prop6
0;1000;89;"a1,a2,a3";33;;
1;;;"a55,a10";1;1 L;87
2;25;1001;a4;;"1,5 L";
You want to use a csv parser. Parsing csv with shell tools is hard (you will encounter regular expressions soon, and they rarely get all cases).
There is one in almost every language. I recommend python.
You can also do this using excel/openoffice variants by opening the file and then saving with ; as the separator.
You can used sed:
echo '0,1000,89,"a1,a2,a3",33,,' | sed -e "s|\"||g"
This will replace " with the empty string (deletes it), and you can pipe another sed to replace the , with ;:
sed -e "s|,|;|g"
$ echo '0,1000,89,"a1,a2,a3",33,,' | sed -e "s|\"||g" | sed -e "s|,|;|g"
>> 0;1000;89;a1;a2;a3;33;;
Note that you can use any separator you want instead of | inside the sed command. For example, you can rewrite the first sed as:
sed -e "s-\"--g"
I have abc.sh:
exec $ROOT/Subsystem/xyz.sh
On a Unix box, if I print echo $HOME then I get /HOME/COM/FILE.
I want to replace $ROOT with $HOME using sed.
Expected Output:
exec /HOME/COM/FILE/Subsystem/xyz.sh
I tried, but I'm not getting the expected output:
sed 's/$ROOT/"${HOME}"/g' abc.sh > abc.sh.1
Addition:
If I have abc.sh
exec $ROOT/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
then with
sed "s|\$INSTALLROOT/|${INSTALLROOT}|" abc.sh
it is only replacing first $ROOT, i.e., output is coming as
exec /HOME/COM/FILE/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
Say:
sed "s|\$ROOT|${HOME}|" abc.sh
Note:
Use double quotes so that the shell would expand variables.
Use a separator different than / since the replacement contains /
Escape the $ in the pattern since you don't want to expand it.
EDIT: In order to replace all occurrences of $ROOT, say
sed "s|\$ROOT|${HOME}|g" abc.sh
This might work for you:
sed 's|$ROOT|'"${HOME}"'|g' abc.sh > abc.sh.1
This may also can help
input="inputtext"
output="outputtext"
sed "s/$input/${output}/" inputfile > outputfile
The safe for a special chars workaround from https://www.baeldung.com/linux/sed-substitution-variables with improvement for \ char:
#!/bin/bash
to="/foo\\bar#baz"
echo "str %FROM% str" | sed "s#%FROM%#$(echo ${to//\\/\\\\} | sed 's/#/\\#/g')#g"
I would need to replace in a file strings like "'a" with strings like 'a.
In practice, I need to remove the double quotes.
I was thinking to use sed to do it, but I could not find a solution til now: I guess I am making some syntax errors because of the quotes.
If you just need to remove all double quote characters from the file then you can use tr with the -d option:
$ cat test.txt
this is a test "'a"
something "else"
doesn't touch single 'quotes'
$ cat test.txt | tr -d '"'
this is a test 'a
something else
doesn't touch single 'quotes'
Update:
If you want to replace the specific instance of "'a" with 'a then you can use sed:
sed "s|\"'a\"|'a|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'
However, I suspect that you are after something more general than just replacing quote markes around an a character. This sed command will replace any instance of "'anything" with 'anyhting:
sed "s|\"'\([^\"]\+\)\"|'\\1|g" test.txt
this is a test 'a
something "else"
doesn't touch single 'quotes'
This seems to work for me
echo '"a"' | sed "s/\"a\"/\'a/"
This might work for you (GNU sed):
sed 's/"\('\''[^"]*\)"/\1/g' file
you could use :
perl -pe 's/\042//g' your_file
042 is octal value of double quotes.
tested below:
> cat temp
"'a"
> cat temp | perl -pe 's/\042//g'
'a
>