This question already has answers here:
Perl substitution using string that contains a dollar sign on windows
(3 answers)
Closed 8 years ago.
I am trying to replace a string "ABC" with ".$VAR." running the below command.
perl -p -i -e "s/ABC/\.\$VAR\./g" *
This makes the string as ".." instead of ".$VAR".Please can anyone tell me what am I doing wrong here??
You don't need to escape . on the replace side since it isn't a
pattern
Just use single quotes around the body so the shell
doesn't interpolate $VAR
Use single quotes:
perl -i -pe 's/ABC/.\$VAR\./g' *
Otherwise, you're only escaping $VAR on the shell and not perl level. If you want to use double quotes, you'll need to use 3 backslashes.
perl -i -pe "s/ABC/.\\\$VAR\./g" *
Perl variables start with $, as well as shell variables. You correctly inserted \ before the $VAR, but it only prevents the expansion of the variable on the shell level, not Perl level. Add more backslashes or switch to single quotes.
perl -i~ -pe "s/ABC/.\\\$VAR./g" *
or
perl -i~ -pe 's/ABC/.\$VAR./g' *
Related
This question already has answers here:
How can I process options using Perl in -n or -p mode?
(2 answers)
Closed 12 months ago.
could someone please explain, why perl doesn't replace the regexp:
root#machine08:~# VERSIONK8S='${VERSION_KUBERNETES:-1.23.3-00}'
# with sed as i want it
root#machine08:~# sed -e "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=${VERSION_KUBERNETES:-1.23.3-00}
# with perl not exactly as i want it
root#machine08:~# perl -pe "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=-e
From Perldoc:
If the delimiter chosen is a single quote, no variable interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time.
Perl tries to expand the substitution string ${VERSION_KUBERNETES:-1.23.3-00} as a perl variable starting with $ and fails. To avoid the expansion, please try:
perl -pe "s'^VERSIONK8S=.*'VERSIONK8S=${VERSIONK8S}'g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
by using a single quote as a delimiter instead of a slash such as s'pattern'replacement'.
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am having a problem while using the bash shell. Here is my linux command code:
for i in `cat linshi`;do sed -i '/$i/d' a.txt;done
The content of linshi is:
aa
bb
The content of a.txt is:
aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg
I want to delete the first and the second row of a.txt.
But unlucky, I found '/$i/d' is not correct. And I have tried '/\$i/d' and '/"\"$id/', but they are fail again. Who can help me?
Variables aren't expanded inside single quotes, only double quotes.
for i in `cat linshi`; do sed -i "/$i/d" a.txt; done
That said, you could do the same thing with:
grep -vf linshi a.txt
Instead of using single quotes use double quotes. '' doesn't undergo any variable expansion however double quotes do.
This will work:
for i in $(cat linshi);do sed -i "/$i/d" a.txt;done
This question already has answers here:
How to escape single quote in sed?
(9 answers)
Closed 6 years ago.
I have a config.js file which contents below strings
.constant('Digin_Engine_API', 'http://local.net:1929/')
I want to read this file and replace what ever the things which are there after the .constant('Digin_Engine_API'I tried using sedbut ddnt worked. This is what I used for sed
sed -i 's/^.constant('Digin_Engine_API', .*/http://cloud.lk:8080/' /var/config.js
As a summary my final out put (config.js) file needs to consists below.
Before
.constant('Digin_Engine_API', 'http://local.net:1929/')
After
.constant('Digin_Engine_API', 'http://cloud.lk:8080/')
You need to use double quotes around sed command since single quote is part of pattern
You should use an alternate delimiter since / is used in replacement
You need to capture the first part and use it in replacement
You need to quote the replacement and also add closing )
Sed command:
sed -i.bak "s~\(\.constant('Digin_Engine_API', \).*~\1'http://cloud.lk:8080')~" /var/config.js
cat /var/config.js
.constant('Digin_Engine_API', 'http://cloud.lk:8080')
Here you are:
sed -i -r "s_(\.constant\('Digin_Engine_API').*_\1, <new content>)_" file
Remarks
you cannot use ' in sed command if command is surrounded by '' also,
you must escape all ( and ) that are part or string, and not the sed grouping command,
you must escape . character, because it is also sed replacement for every char,
you must use another sed s separator instead of / if you need to use / in that command, but you can also escape / by \/.
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 6 years ago.
i would insert two variable in sed command:
sed -i '39,41 s/^#//' file
i would
sed -i '$LINE,$LINE_INCREMENTED s/^#//' file
but return this:
sed: -e expression #1, char 9: unknown command: `$'
Shell variables are not expanded when put inside single quotes, they are treated literally then.
Do:
sed -i "$LINE,$LINE_INCREMENTED"' s/^#//' file
Assuming the variables only contain digits.
As s/^#// part does not contain any shell expansion, putting double quotes over the full expression would do too, better readability:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
drop the quotes for double quotes so environment variables are evaluated:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 8 years ago.
I have an xml file with line below
<config-root>/temp</config-root>
Using 'sed' is bash shell script I want to replace the line,
the 'sed' script is below
sed -i 's/<config-root>\(.*\)<\/config-root>/<config-root>\"${dirPath}"<\/config-root>/' Plan.xml
The 'sed' is resulting in
<config-root>"${dirPath}"</config-root>
I am expecting the line to be replaced as /opt/shared
Can anyone let me know what is wrong in my script? Basically I want to use variable in ‘sed’
Thanks in advance,
Babu
You can use bash to place the variable in the sed script: End the sed script using the single quote ', place the variable in double quotes " and continue the sed program with another single quote ':
sed 's~<config-root>[^<]*</config-root>~<config-root>'"$variable"'</config-root>~' Plan.xml
I would encourage you to use delimiter different from / because the / is part of the pattern (and of the variable) and would need to get escaped otherwise. I used ~ as the delimiter.