1

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
user3388884
  • 4,748
  • 9
  • 25
  • 34

2 Answers2

3

Use double quotes for variable expansion in sed

sed -n "/^$date/,\$p" filename
Amit
  • 19,780
  • 6
  • 46
  • 54
  • The `$p` is not a variable, it's EOF (`$`) and a command (`p`), so the `$` needs to be escaped. – Kevin Mar 06 '14 at 15:57
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
Kevin
  • 53,822
  • 15
  • 101
  • 132