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'.
Related
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have the following string:
/book/A00001/2018/01/15/Chamber_Music
And I want to get using the sed command:
/book/A00001/2018/01/15/
Thanks
Regards
Maybe are you looking for: sed "s/\(.*\)Chamber_Music/\1/g"
No need to use sed, you can use normal shell string handling:
filename='gash.txt'
new_filename="$filename.new"
while read line
do
line=${line%/*}
echo $line
done <"$filename" >"$new_filename"
#mv "$new_filename" "$filename" # Commented out to be optional
Given your input in your second question:
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
You can change the regexp sed delimiter.... see the s command documentation. If you use, e.g.
sed 's:([a-zA-Z0-9/]*)[a-zA-Z]*$:\1:'
then, the / loses its special treatment and the : character assumes it. Of course, you can store the matching pattern in an environment variable, before to feed it to sed(1), and substitute all / into \/ to escape every /.
pattern=`echo "([a-zA-Z0-9/])[a-zA-Z]*\$" | sed 's/\//\\//'`
sed "s/${pattern}/\\1/"
but this is more complex.
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:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...
Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I'm trying to replace text in a file with the output of another command. Unfortunately, the outputted text contains characters bash expands. For example, I'm running the following script to change the file (somestring references output that would break the sed command):
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt
sed fails with this error:
sed: -e expression #1, char 30: unknown option to `s'
I think bash is substuting the contents of $somestring when building the sed command, but is then trying to expand the resulting text. I can't put the entire sed script in single quotes, I need bash to expand it the first time, just not the second. Any suggestions? Thanks
here the forward slash / is the problem. If it's the only issue you can set sed to use a different delimiter.
for example
$ somestring="abc/def"; echo xxx | sed 's/xxx/'"$somestring"'/'
sed: -e expression #1, char 11: unknown option to `s'
$ somestring="abc/def"; echo xxx | sed 's_xxx_'"$somestring"'_'
abc/def
you also need to worry about & and \ chars and escape them if can appear in the replacement text.
If you can't control the the replacement string, either you have to sanitize with another sed script or, alternatively use r command to read it from a file. For example,
$ seq 5 | sed -e '/3/{r replace' -e 'd}'
1
2
3slashes///1ampersand&and2backslashes\\end
4
5
where
$ cat replace
3slashes///1ampersand&and2backslashes\\end
You have several errors here:
the string somestring has characters that are significative for sed command (the most important being '/' that you are using as a delimiter) You can escape it, by substituting it with a previous
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
that will convert your / chars to \/ sequences.
you are using sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt which is looking to substitute the string titl followed by any number of e characters by that $somestring value, followed by a new line and the original one. Unfortunately, sed(1) doesn't allow you to use newline characters in the pattern substitution side of the s command, but you can afford the result by using the i command with a text consisting of you pattern (preceding any new line by a \ to interpret it as literal):
Finally the script leads to:
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
sed '/^title/i\
'"$somestring\\
" $HOME/example.txt
If your shell is Bash, you can use parameter substitution to replace the problematic /:
somestring="{somestring//\//\\/}"
That looks scary, but is easier to understand if you look at the version that replaces x with __:
somestring="${somestring//x/__}"
It might be easier to use (say) underscore as the delimiter for your sed s command, and then the substitution above would be
somestring="${somestring//_/\\_}"
If you already have backslashes, you'll need to first replace those:
somestring="${somestring//\\/\\\\}"
somestring="{somestring//\//\\/}"
If there were other characters that needed escaping (e.g. on the search side of s///), then you could extend the above appropriately.
This URL provides the cleanest answer:
Command to escape a string in bash
printf "%q" "$someVariable"
will escape any characters you need escaped for you.
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' *