更准确地说-如何将参数传递到随后调用sed的程序中-如何“传递”该参数?
我有一个文件“ source_code.sc”。
我有一个sed脚本“ find_expect”:
#n
/expect/{
=
p
}
我是从一个程序内部调用的,该程序通过以下方式遍历文件
sed -f find_expect.= source_code.sc
该印刷品找到其行号,例如
712
expect(it).to be_true
但是,搜索是硬编码的,用于搜索“期望”。如何更改此设置,以便搜索文本本身也可以通过。我还使用find遍历文件,因此当前对许多文件调用sed。例如,要搜索“ blob”,程序在文件中循环将使用以下命令调用它
sed -f find_expect.= "$file_from_loop" source_code.sc 'blob'
# with 'blob' being passed in from the main script
# which would be invoked with `./change_all.sh 'blob'
# I don't pass the file pattern in to this main call as I just
# recursively do all files (that match a pattern) from the current directory down
听起来您正在尝试重新实现grep
。而不sed
只是使用:
grep -n -- "$var" file
如果var
包含要从字面上进行匹配的正则表达式元字符,请提供以下-F
选项grep
:
grep -nF -- "$var" file
其中var
包含您所需的模式。
从grep(1)
手册页:
-n,--
line-number在其输出文件的每个输出行之前都以从1开始的行号作为前缀。(-n由POSIX指定。)
如果您坚持使用sed
,则可以为此目的创建一个函数:
notgrep (){
if [ $# -ne 2 ];then
echo "Usage: notgrep <pattern> <file>"
exit 1
end
sed -n "/$1/{
=
p
}" "$2"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句