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,208
  • 6
  • 22
  • 34
  • Welcome to SO. You've made a good first impression. Please read this next: http://stackoverflow.com/help/someone-answers – glenn jackman Mar 06 '14 at 16:01

2 Answers2

3

Use double quotes for variable expansion in sed

sed -n "/^$date/,\$p" filename
Amit
  • 18,038
  • 6
  • 44
  • 52
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
  • 51,293
  • 15
  • 96
  • 128