0

我想做的是:

>STRIPPER='sed s/admin_editable=\"[01]\"// | sed s/runtime_editable=\"[01]\"//'
>cat file.txt | $STRIPPER > stripped.txt  

即定义一个shell变量,它是多个命令的管道(我的恰好是sed's),然后我可以稍后调用。我现在从命令行执行此操作,但最终可能会将其放入脚本中。

我已经尝试了 ' 和 " 来包含命令都不起作用。

sed: can't read |: No such file or directory
sed: can't read sed: No such file or directory
sed: can't read s/admin_editable=\"[01]\"//: No such file or directory
sed: can't read |sed: No such file or directory
sed: can't read s/runtime_editable=\"[01]\"//: No such file or directory
sed: can't read |: No such file or directory

我知道可能有一个正则表达式可以处理这种情况,但我想知道如何做一般的管道。

4

1 回答 1

3

这是使用函数而不是变量的好地方。

stripper() {
    sed s/admin_editable="[01]"// | sed s/runtime_editable="[01]"//
}

cat file.txt | stripper > stripped.txt  

您还可以消除 cat 的无用用法:

stripper < file.txt > stripped.txt 
于 2013-02-05T17:49:59.490 回答