I am attempting to create a new debug installation script from an existing one. The original installation script contains many statements with redirection >/dev/null 2>&1
. It includes different patterns so I found the regex expression that matches all:
>{1,2}\s{0,1}\/dev\/null 2>\&1
I've tested this regex here .
Now I'd like to use this pattern to find and replace all instances of matches with a blank so that in case there are any errors, the output stream will be redirected to the terminal rather to the null
device. My attempt is:
sed 's+>{1,2}\s\/dev\/null 2>\&1++g' install.sh > install_debug.sh
(I used the +
instead of /
as the delimiter to make the string more readable)
But when I grep the output file or just pipe the output without creating the file, /dev/null
shows that the substitution was not successful:
sed 's/>{1,2}\s\/dev\/null 2>\&1//g' install.sh | grep "null"
elif type lsb_release >/dev/null 2>&1; then
if lsof /var/lib/dpkg/lock >> /dev/null 2>&1; then
sudo apt-get update > /dev/null 2>&1 && \
sudo DEBIAN_FRONTEND=falseninteractive apt-get install -y sshpass python-minimal python-pip dbus > /dev/null 2>&1 && \
sudo apt-get install -y jq > /dev/null 2>&1
sudo python_for_core_os > /dev/null 2>&1
Any ideas what I'm doing wrong?
Thanks!
You are having \s
in an attempt to match 1 or more between >
and /dev
, but the strings can have more or none. Besides, you are not escaping braces in the POSIX BRE pattern, so sed 's/a{1,2}//'
effectively removes the first a{1,2}
text, not one or two a
s. Either use a POSIX ERE (with the -E
option) to use a{1,2}
, or escape the braces (ie sed -E 's/a{1,2}//'
= sed 's/a\{1,2\}//'
).
Also, &
in the regex pattern is not special, it is only special in the replacement part. Thus, no need escaping it here. Also, since you used +
as a delimiter, there is no point escaping /
chars any longer, replace \/
with /
.
Use
sed 's+>\{1,2\}[[:space:]]*/dev/null[[:space:]]*2>&1++g' install.sh > install_debug.sh
In a GNU sed
you may surely use \s
to shorten the pattern:
sed -E 's+>{1,2}\s*/dev/null\s*2>&1++g' install.sh > install_debug.sh
See the online sed
demo
You have to escape the {
:
$ sed 's#>\{1,2\}\s*/dev/null\s*2>\&1##g' install.txt
elif type lsb_release ; then
if lsof /var/lib/dpkg/lock ; then
sudo apt-get update && \
sudo DEBIAN_FRONTEND=falseninteractive apt-get install -y sshpass python-minimal python-pip dbus && \
sudo apt-get install -y jq
sudo python_for_core_os
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.