using sed for substitution in next line - sed

I am working on sed command to translate some text into another text.
cat text
<strong>ABC
</strong>
Commnad:
sed -e 's|<strong>(.*?)</strong>|//textbf{1}|g'
Expected Outcome: \textbf{ABC}
but using above script i cannot convert it into expected output since there is new line between the tags. How to handle such cases?

This might work for you (GNU sed):
sed -r '$!N;s|(<)(strong>)([^\n]*)\n\s*\1/\2|//textbf{\3}|;P;D' file
or
sed '$!N;s|\(<\)\(strong>\)\([^\n]*\)\n\s*\1/\2|//textbf{\3}|;P;D' file

sed -e 'N;s|<strong>\(.*\?\)\n</strong>|\/textbf{\1}|g'
as said by CodeGnome and David Ravetti, the N flag allows for multi-line patterns.

Related

Replacing the test with sed

I'm trying to replace the text using the sed, but it's showing some error. Not getting where I'm getting wrong.
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/\callback/g' input.txt
Have this :
process.env.REDIRECT_URI
Replace this with :
http://test-domain.apps.io
Try:
sed -i 's/process.env.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Notes:
The original command has a spurious string /\callback. All that was needed to make the code work was to remove it.
. is a wildcard. If you want to be sure that you are matching periods, they should be escaped:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io/g' input.txt
Sometimes, its clearer if one doesn't have to escape /. One can use a separator of one's choice. For example, use #:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io#g' input.txt
If you did want /callback in the output, use:
sed -i 's/process\.env\.REDIRECT_URI/http:\/\/test-domain.apps.io\/callback/g' input.txt
or:
sed -i 's#process\.env\.REDIRECT_URI#http://test-domain.apps.io/callback#g' input.txt

Trying to overwrite a string that has single quotes using Perl or sed in the terminal

I have the following line in a file:
$app-assets:"/assets/";
I am trying to use sed in the terminal to overwrite that line to read as follows:
$app-assets:"http://www.example.com/assets/";
I have tried the following but it does not work:
sed -i \'\' -e \'s/app-assets:"/assets/"/app-assets:"http://www.example.com/assets/"/g\' myfile.txt
I am fine using Perl if easier.
Use the following sed approach:
sed -i 's~\(\$app-assets:"\)\(/assets/\)"~\1http://www.example.com\2"~' myfile.txt
~ here is treated as sed subcommand separator
sed 's/app-assets:\"\/assets\/\";/app-assets:\"http:\/\/www\.example\.com\/assets\/\";/g' filename

SED command to replace strings with in file

I am trying to use sed to replace strings with special characters in a text file. The sed command is becoming too much complicated. If someone could please help me with the exact command.
Code -
sed -i 's;PS1='${HOSTNAME} [$ORACLE_SID] $PWD> ';PS1="${COL_YELLOW}'CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '${COL_END}";g'
I tried to escape the special characters as below but its not working.
sed -i 's;PS1=\'\${HOSTNAME} [\$ORACLE_SID] \$PWD> \';PS1="\${COL_YELLOW}\'CUSTOMER TEST:\${HOSTNAME}:[\$ORACLE_SID]:\$PWD> \'\${COL_END}";g' .bash_profile_backup
This might work for you (GNU sed):
sed -i 's|PS1='\''${HOSTNAME} \[$ORACLE_SID\] $PWD> '\''|PS1="${COL_YELLOW}'\''CUSTOMER TEST:${HOSTNAME}:[$ORACLE_SID]:$PWD> '\''${COL_END}"|g' file
N.B. ' need to be quoted in both the pattern and replacement whereas [] needs to be escaped in the pattern only.

Combine -e and -n sed options

I'm trying to convert all occurrences of a certain letter in the header of a file to lowercase, i can achieve this with 2 sed lines but i would like to use one instead.
What i'm trying is this:
cat file.txt | sed -e 'n 1p' -e 's/U/u/g'
Supposing that the letter i want to replace is the 'u'
I feel like i'm very close to it but for some reasons i get sed to complain about an extra char after the 'n' command, but in this case -n needs a parameter, so there should be no reason to complain.
Any hint?
This might work for you (GNU sed):
sed '1y/U/u/' file
try this (GNU sed):
sed '1s/U/u/g' file

sed - substitute either of two characters with one command

I would like one sed command to accomplish the following:
$ sed s'/:/ /g' <and> sed s'/=/ /g'
That is, I would like to write
sed s'/<something>/ /g'
and have both = and : replaced by space.
sed s'/[:=]/ /g'
Brackets mean "any one of".
One option is also to use sed -e, like this. Although you don't need it in this case, it's however a good option to know about.
sed -e 's/:/ /' -e 's/..../ /' file
Sanjay's answer solves it. Another option that works with only one sed command is to separate each s substitution with a semicolon
sed 's/:/ /g ; s/=/ /g' file
or in separate lines in a script
sed 's/:/ /g
s/=/ /g' file
Those may be handy in other situations.