2

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
0

2 Answers 2

5

Use double quotes for variable expansion in sed

sed -n "/^$date/,\$p" filename
Sign up to request clarification or add additional context in comments.

1 Comment

The $p is not a variable, it's EOF ($) and a command (p), so the $ needs to be escaped.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.