使用命令行参数的sed命令 [英] use commandline arguments in a sed command
问题描述
我想在一个sed命令,在那里我目前包裹论点''使用命令行参数。我的命令是:
I want to use commandline arguments in a sed command, where I am currently wrapping the arguments in ''. My command is:
mycomputer$ sed 's/^\(.\{'$1'\}\)./\1'$2'/' out2.fa 2 A
基本上着眼于一个文件中的一行,并改变第二位到A.这工作时,我很难code它,但在上述结果:
which essentially looks at a line in a file, and changes the 2nd position to an A. This works when I hardcode it in, but the above results in:
sed: -e expression #1, char 21: Invalid content of \{\}
也应该我使用$ 2和$ 3作为命令行参数给出的文件名是技术上的第一个参数?
also should I be using $2 and $3 as the commandline arguments given that the filename is technically the first argument?
推荐答案
我不知道该 $ 1
, $ 2
...等可以从命令行进行访问。
I'm not sure that $1
, $2
… and so on can be accessed from command line.
相反,你可以尝试从一个函数运行线。你绝对可以传递参数的功能。
Instead, you could try to run your line from a function. You definitely can pass argument to functions.
foo() { sed 's/^\(.\{'"$2"'\}\)./\1'"$3"'/' "$1"; }
foo out2.fa 2 A
修改:如意见提出,我说周围的参数引用prevent空间双引号破裂命令解析(这不应该是各地必要 $ 2
正如我们刚才整数关口,但还有更好的 $ 1
和 $ 3
。
Edit: As suggested in comments, I added double-quotes around the argument references to prevent spaces from breaking command parsing (That should not be necessary around $2
as we just pass an integer, but it's much better for $1
and $3
.
这篇关于使用命令行参数的sed命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!