How to use variables in linux regex pattern?

ぐ巨炮叔叔 提交于 2021-01-28 20:57:56

问题


I need to execute a sed command with pattern /^03.06.2014/ in a .sh script & command line. The date is a variable not a constant. How could i implement this? When I use a variable inside a regex pattern, the command breaks. Do I need to escape something here? Any help is appreciated. Thanks!

date=$(date +%m.%d.%Y)
sed -n '/^$date/,$p' filename

回答1:


Use double quotes for variable expansion in sed

sed -n "/^$date/,\$p" filename



回答2:


You need to use double quotes to allow for shell expansion. You'll need to escape the $ meaning EOF.

sed -n "/^$date/,\$p" filename


来源:https://stackoverflow.com/questions/22229449/how-to-use-variables-in-linux-regex-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!