2013-02-05 42 views
0

我想要做的是:连接多个命令

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

即定义一个shell变量是多个命令的管道(我碰巧sed的),我然后可以再打电话。我现在正在从命令行执行此操作,但最终可能会将其放入脚本中。

我都试过“和”为

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 

封闭命令既不的作品。我知道有可能是一个单一的正则表达式,可以处理这种情况,但我想知道如何做管道一般

+0

流水线是shell语法的一部分,因此在任何参数扩展发生之前都会被识别出来。 – chepner

回答

3

这是使用功能,而不是一个变量的好地方

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

cat file.txt | stripper > stripped.txt 

你也可以消除useless use of cat:。

stripper <file.txt> stripped.txt