问题
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