writing the values of variables to file. the value is got changed automatically - bash

temp="This is the \\, example"
perl -pi -e "s/new.*/$temp/" tmp.txt
output:
cat tmp.txt
This is the \, example
In the above example the output should contain double \. but it contain only one \.
is there is any reason?

Backslash is the escape character inside double quotes. Use a single quote to disable escaping.
It's also an escape character in perl regexp and replacement strings, so you need to double it to make it expand literally.
temp='This is the \\\\, example'
perl -pi -e "s/new.*/$temp/" tmp.txt

temp= "new \\\\,"
cat tmp.txt
This is old Text
perl -pi -e "This.*/$temp/g" tmp.txt
cat tmp.txt
This is new\,Text
Still im getting same output after adding \\\\.

Related

gsed replace by a variable $i with single quote

I have into a text file the following line :
\[Omega]BD=100;
I would like to replace with gsed the value 100 by a shell variable (zsh shell), here 600 :
I tried :
$ i=600
$ gsed 's/\[Omega]BD=.*/\[Omega]BD=\'\\"$i"\\';/' text_to_modify.txt | grep 600
but it returns me :
\[Omega]BD=\600; and not \[Omega]BD=600;
The is an additional backslash that I don't want, I wonder how could I remove this backslash. I would like to keep the two single quotes of gsed 's/.../.../'
Using sed;
i=600
$ sed "/\[Omega]/s/[[:digit:]]\+/$i/" input_file
\[Omega]BD=600;
You may use this sed command:
i=600
sed -E "s/(\\\\\[Omega]BD=).*/\1$i;/" file
\[Omega]BD=600;
We require additional escaping i.e. \\\\ to match a single \ because we are using double quotes around full sed command.
Or we can avoid you can use this combination of single and double quotes to avoid extra escaping:
sed -E 's/(\\\[Omega]BD=).*/\1'"$i;/" file

sed ' in replacement text when $ in search pattern

I have a file containing the string "this $c$ is a single quote", created as follows:
%echo "this \$c\$ is a single quote" > test3.txt
%cat test3.txt
this $c$ is a single quote
I would like to replace the letter c by a single quote, but I need to match the $ characters as well (to avoid matching other characters 'c'). I can't seem to do this.
I tried
%sed 's/$c$/$foo$/' test3.txt
this $c$ is a single quote
so obviously I need to escape the $.
%sed 's/\$c\$/$foo$/' test3.txt
this $foo$ is a single quote
But when I try to put an escaped ' in the replacement text I get
%sed 's/$c$/$\'$/' test3.txt
quote>
So I need to use some other quoting method.
%sed "s/\$c\$/$'$/" test3.txt
this $c$ is a single quote
Nothing was replaced, so let's try not escaping the $
%sed "s/$c$/$'$/" test3.txt
this $c$ is a single quote$'$
That was unexpected (to me), so let's try matching just the c as a sanity check.
%sed "s/c/'/" test3.txt
this $'$ is a single quote
I tried a number of other combinations but no luck. How do I do this in sed?
How about this?
!$ echo 'This is $c$ ceee' | sed s/\\\$c\\\$/\'/
This is ' ceee
I do not enclose the whole sed's command in quotes, so I need to escape each backslash and each dollar separately (and the quote as well, of course).
Edit
As Chris Lear points out, my replace string contains no dollars. Here is a fix – please note these dollars do not have a special meaning for sed (they are not interpreted as symbols for match, they're just plain characters to be inserted) so they can be escaped only once:
!$ echo 'This is $c$ ceee' | sed s/\\\$c\\\$/\\\$\'\\\$/
This is $'$ ceee
!$ echo 'This is $c$ ceee' | sed s/\\\$c\\\$/\$\'\$/
This is $'$ ceee
If you want to quote the sed command you need to do plenty of escaping. $ is a special character for both the shell and the sed patterns. In the shell it means the start of a variable name to expand, $c in your case. To sed it means the end of the line. To do the quoting you need to escape it from both of those so you could do
sed "s/\\\$c\\\$/\$'\$/" test3.txt
or you could mix your quoting styles to use single quotes around the $ expansions and double quotes around your single quote like
sed 's/\$c\$/$'"'"'$/' test3.txt
You can use ansi c string
sed $'s/\$c\$/\'/'
This allows single backslash escaping of $s and 's.
More info here
If you want to keep $s
sed $'s/\$c\$/$\'$/'
Try this :
sed -i -E "s/([$]c[$])/\'/g" sed.txt

How to pass special characters through sed

I want to pass this command in my script:
sed -n -e "/Next</a></p>/,/Next</a></p>/ p" file.txt
This command (should) extract all text between the two matched patterns, which are both Next</a></p> in my case. However when I run my script I keep getting errors. I've tried:
sed -n -e "/Next\<\/a\>\<\/p\>/,/Next<\/a\>\<\/p>/ p" file.txt with no luck.
I believe the generic pattern for this command is this:
sed -n -e "/pattern1/,/pattern2/ p" file.txt
I can't get it working for Next</a></p> though and I'm guessing it has something to do with the special characters I am encasing. Is there any way to pass Next</a></p> in the sed command? Thanks in advance guys! This community is awesome!
You don't need to use / as a regular expression delimiter. Using a different character will make quoting issues slightly easier. The syntax is
\cregexc
where c can be any character (other than \) that you don't use in the regex. In this case, : might be a good choice:
sed -n -e '\:Next</a></p>:,\:Next</a></p>: p' file.txt
Note that I changed " to ' because inside double quotes, \ will be interpreted by bash as an escape character, whereas inside single quotes \ is just treated as a regular character. Consequently, you could have written the version with escaped slashes like this:
sed -n -e '/Next<\/a><\/p>/,/Next<\/a><\/p>/ p' file.txt
but I think the version with colons is (slightly) easier to read.
You need to escape the forward slashes inside the regular expressions with a \, since the forward slashes serve as delimiters for the regexes
sed -n -e '/Next<\/a><\/p>/,/Next<\/a><\/p>/p' file.txt

How to replace ' with sed?

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

single quote inside double quotes in shell script

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
>

Resources