SED replace a word inside a bracket [duplicate] - linux

This question already has answers here:
What is the difference between `sed -i -e` and `sed -ie`?
(2 answers)
Closed 2 years ago.
I need to change some words in bulk, but because of the brackets inside it, I think I do something wrong.
Line that needs to be changed
echo "CMD_PLUGINS_ADMIN/admin/index.html";
I need to change it to this:
echo "CMD_PLUGINS_ADMIN/reseller/index.html";
I tried it with: sed -ie 's/admin/reseller/' *
But does not change anything, I hope someone knows the right command for it.

$ echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

your input has slash and you are using slash as sed seperator
Either escape the slashes in input by preceeding them with backslash:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
OR change seperator to any other like pipe:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's|admin|reseller|g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

Related

Bash script find and replace string with variable that contains complex special characters without having to escape? [duplicate]

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 3 months ago.
Is there a simpler way to replace a string with a variable literally that contains all complex characters without needing to do character escaping?
complex='~`!##$%^&*()-_=+\|]}{[;:/?.>,<\/*&\#/'
sed -i "s/secret\ =\ \".*/secret\ =\ \"$complex\"/g" ./file.txt
I already know that & \ and / are the problems. I can do all manner of cleaning with sed commands, but is there a simpler way? Is there a way I can literally make sed read that variable as is?
My work around for now is the following, but even this does not work with / properly ...
complex='~`!##$%^&*()-_=+\|]}{[;:/?.>,<\/*&\#/'
psk_bs="$(echo $complex | sed 's/\\/\\\\\\/g')"
psk_bs_amp="$(echo "$psk_bs" | sed 's/\&/\\&/g')"
psk_bs_amp_fs="$(echo "$psk_bs_amp" | sed 's,'/','\/',g')"
sed -i "s/secret\ =\ \".*/secret\ =\ \"$psk_bs_amp_fs\"/g" ./file.txt
I can do all manner of cleaning with sed commands, but is there a simpler way? Is there a way I can literally make sed read that variable as is?
I'm afraid there is not. But character escaping is not as a big deal as you make it seem to be. This'll work just fine:
psk=$(sed 's/[/&\]/\\&/g' <<< $complex)
sed -i "s/secret = \".*/secret = \"$psk\"/g" ./file.txt
With bash 5.2 you don't even need sed for escaping:
psk=${complex//[\/&\\]/\\&}

Print new line as string literal in unix or shell [duplicate]

This question already has answers here:
How to replace one character with two characters using tr
(5 answers)
Closed 3 years ago.
Hi I have a shell script that has
variable="apple banana monkey"
I want it to be
apple\nbanana\nmonkey
But when I try and execute
echo $variable | tr ' ' '\n'
It results to
apple
banana
monkey
I want to get the actual literal of new line and not the evaluated value.
I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.
Please help. Thanks
tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.
If you accept a command switch, you can try with sed:
echo "$variable" | sed 's/ /\\n/g'

How to define $i in linux bash shell? [duplicate]

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

how to use sed command properly to replace values containing / delimiter [duplicate]

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.

Cannot redirect stdout to file specified by full path [duplicate]

This question already has answers here:
why sed replace + redirection deletes my file?
(3 answers)
Closed 7 years ago.
Let
CONFIG_FILE=${WWW_DIR}/${WORDPRESS_TEST_DIR}/wp-config.php
This doesn't work:
sed "${SED_ARG}" ${CONFIG_FILE} >| ${CONFIG_FILE}
(all I get is an empty file). It does not work either when I replace the variables with the actual path strings, nor when I escape the paths with ".
This does work:
sed "${SED_ARG}" ${CONFIG_FILE} >| wp-config.php
mv wp-config.php ${CONFIG_FILE}
so I would assume the sed call works just fine.
Strangely, this does work too:
echo TEST >| ${CONFIG_FILE}
so the sed part might be the problem after all. I am clueless. Any ideas?
The redirection opens the file for writing (not appending), which makes it a new empty file, before the sed command is executed.
You can use the -i flag of sed, to do an in place substitution:
sed -I "${SED_ARG}" "${CONFIG_FILE}"
I also put quotes around ${CONFIG_FILE} to avoid unexpected behavior if it contains any special characters.

Resources