【问题标题】:sed error complaining "extra characters after command" [duplicate]sed错误抱怨“命令后有多余的字符”[重复]
【发布时间】:2014-06-17 20:44:37
【问题描述】:
#!/bin/bash

# Let's say now, we are working in my $HOME directory

# Content of testfile (originally)
# 123456
# ABCDEF
# /home/superman

string="ABCDEF"
myfile="$HOME/testfile"

# test-1, this is okay
sed -i "/$string/d" $myfile
echo $string >> $myfile

# test-2, this fails
# ERROR (sed: -e expression #1, char 4: extra characters after command)
sed -i "/$PWD/d" $myfile
echo $PWD >> $myfile

# Not working either
sed -i ":$PWD:d" $myfile
echo $PWD >> $myfile

我的问题:如何处理 $PWD 的情况?

【问题讨论】:

    标签: bash shell sed


    【解决方案1】:

    要为地址使用备用分隔符,您需要使用反斜杠 - \

    sed "\:$PWD:d" < $myfile
    

    应该可以。

    当然对于这个确切的例子,grep -v 可能更简单。

    【讨论】:

    • 使用\: 会好很多。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2017-01-08
    • 1970-01-01
    相关资源
    最近更新 更多