做的sed只有存在变数删除 [英] do sed to delete only if exist variable

查看:107
本文介绍了做的sed只有存在变数删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的前pression:

  //最初变量,之前设置的过程HOSTS_APACHE =HOSTS =//一些过程...出口HOSTS_CANAL_TMP = $(回声$ HOSTS | sed的S / $ HOSTS_APACHE //)

它运作良好,当Apache主机的主机组(后处理)的存在。但是,当我有一组主机( $ HOSTS )的不工作,我没有任何Apache主机排除。

所以我需要一个类似的前pression的情况下,做 $ HOSTS_APACHE 的SED中有 $ HOSTS阿帕奇,并没有做任何事情的情况下有没有。

现在当没有 $ HOSTS_APACHE $ HOSTS排除

错误

 的sed:-e前pression#1,烧焦0:无previous定期EX pression


解决方案

编辑:如果我收集OP正确,这个问题 - 或者一个次要的问题 - 是HOSTS_APACHE是无序的空间分隔的主机或主机名的列表。在这种情况下,该解决方案是这样的:

 如果[-n$ HOSTS_APACHE];然后
    HOSTS_CANAL_TMP = $(回声$ HOSTS | sed的S / $ {// HOSTS_APACHE / \\\\ |} // G)
其他
    HOSTS_CANAL_TMP = $ HOSTS
科幻

说明:

如果 HOSTS_APACHE 栏巴兹然后 $ {// HOSTS_APACHE / \\ \\ |} 巴兹\\ |栏中的。使用双的变量扩展// 是全球替换(空格字符)与 \\ |

旧文章:

有做到这一点的方法不止一种:

  HOSTS_CANAL_TMP = $(回声$ HOSTS |
    如果[-n$ HOSTS_APACHE];然后
        SEDS / $ HOSTS_APACHE //
    其他
        猫#通过
    FI)

[-n STRING] 返回0,如果字符串拥有的 N 导通长度为零。

究其原因 |如果...;网络的工作原理是因为,如果结构作为接收STDIN并写到标准输出的一个缩影程序。哪个程序将调用处理I / O是由如果子句选择。

另外,你可以使用的if-then-else结构的子shell之外:

 如果[-n$ HOSTS_APACHE];然后
    HOSTS_CANAL_TMP = $(回声$ HOSTS | sed的S / $ HOSTS_APACHE //)
其他
    HOSTS_CANAL_TMP = $ HOSTS
科幻

I have this expression:

// Initially variables set to "" before process

HOSTS_APACHE=""

HOSTS=""

// Some process ...

export HOSTS_CANAL_TMP=$(echo $HOSTS | sed "s/$HOSTS_APACHE//")

It works well when apache hosts exists in a group of hosts (after processing). But it doesn't work when I have a group of hosts ($HOSTS) and I don't have any apache host to exclude.

So I need a similar expression to do SED of $HOSTS_APACHE in case there are apaches in $HOSTS and don't do anything in case there aren't.

Now the error when there aren't $HOSTS_APACHE to exclude in $HOSTS

sed: -e expression #1, char 0: no previous regular expression

解决方案

Edit: If I gather OP correctly, the issue -- or perhaps a secondary issue -- is that HOSTS_APACHE is an unordered, space-separated list of hosts or host names. In that case the solution is thus:

if [ -n "$HOSTS_APACHE" ]; then
    HOSTS_CANAL_TMP=$(echo $HOSTS | sed "s/${HOSTS_APACHE// /\\|}//g")
else
    HOSTS_CANAL_TMP=$HOSTS
fi

Explanation:

If HOSTS_APACHE is "bar baz" then ${HOSTS_APACHE// /\\|} will be "baz\|bar". The variable expansion using the double // is a global replacement of " " (space character) with \|.

Old post:

There's more than one way to accomplish this:

HOSTS_CANAL_TMP=$(echo $HOSTS |
    if [ -n "$HOSTS_APACHE" ]; then
        sed "s/$HOSTS_APACHE//"
    else
        cat # pass through
    fi)

[ -n STRING ] returns 0 if STRING has a non-zero length.

The reason |if ...; fi works is because the if structure acts as a miniature program that receives STDIN and writes to STDOUT. Which program it invokes to process I/O is chosen by the if clause.

Alternatively, you could use the if-then-else structure outside the subshell:

if [ -n "$HOSTS_APACHE" ]; then
    HOSTS_CANAL_TMP=$(echo $HOSTS | sed "s/$HOSTS_APACHE//")
else
    HOSTS_CANAL_TMP=$HOSTS
fi

这篇关于做的sed只有存在变数删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆