sed - substitute either of two characters with one command - sed

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.

Related

remove special character (^#) with sed

I want to remove "^#^#^#^#^#^#^#^#^#" from my textfile. I tried the following, but it did not work:
sed -i 's/\\^#//g' myfile.txt
sed is not generally robust against null characters. But Perl is, and tr:
tr -d '\000' <myfile.txt >newfile.txt
Some sed variants will be able to handle null bytes with the notation which works in Perl:
perl -i -pe 's/\x00//g' myfile.txt
The -i option says to replace the original file, like some sed variants also allow you to.
^# is one of the ways how to display the null byte. sed (at least the GNU one) represents it as \x00:
sed 's/\x00//g'

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

Need to parse the following sed command: sed -e 's/ /\'$'\n/g'

I stumble upon the command sed -e 's/ /\'$'\n/g'that supposedly takes an input and split all spaces into new lines. Still, I don't quite get how the '$' works in the command. I know that s stands for substitute, / / stands for the blank spac, \n stands for new line and /g is for global replacement, but not sure how \'$' fits in the picture. Anybody who can shed some light here will be much appreciated.
Basically it's meant for platform portability. With GNU sed it would be just
sed -e 's/ /\n/g'
because GNU sed is able to interpret \n as new line.
However, other versions of sed, like the BSD version (that comes with MacOS) do not interprete \n as newline.
That's why the command is build out of two parts
sed -e 's/ /\' part2: $'\n/g'
The $'\n/g' is an ANSI C string parsed by the shell before executing sed. Escape sequences like \n will get expanded in such strings. Doing so, the author of the command passed a literal new line (0xa) to the sed command rather than passing the escape sequence \n. (0x5c 0x6e).
One more thing, since the newline (0xa) is a command separator in sed, it needs to get escaped. That's why the \ at the end of the first part.
Alternatively you could just use a multiline version:
sed -e 's/ /\
/g'
Btw, I would have written the command like
sed -e 's/ /\'$'\n''/g'
meaning just putting the $'\n' into the ANSI C string. Imo that's better to understand.

replace spaces or tabs with semicolon with sed

how can I replace a tab or empty spaces with a single semicolon?
This is what I'm trying:
sed -i.bak 's/(\s)+|(\t)+/,/g' aws_lab.txt
+/ was unexpected at this time.
Thanks
Just use the appropriate POSIX character class:
sed 's/[[:space:]]\{1,\}/,/g' aws_lab.txt
Very few seds will recognize the \s shorthand for [[:space:]] and since a tab is one of the characters included in [:space:] you dont need to specify it separately. Also, + is an ERE metacharacter, not BRE as supported by sed by default so you'd need to add the -E arg to sed to use it (only supported by GNU and OSX sed variants).
This might work for you (GNU sed):
sed -i.bak 's/[ \t][ \t]*/:/' file
Or
sed -ri.bak 's/[ \t]+/:/' file
Where there is an explicit space infront of the tab:
sed -i.bak 's/[ \t][ \t]*/:/g' file
Will replace repeated patterns throughout a line.

using sed for substitution in next line

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.