-1

I can't get this sed command to work.

remote_file_path=$(tac $local_file_name | sed -nr '1,/$file_name/ d; /^\\/ { p; q }')

If I replace the single quotes with doubles, it breaks the rest of the command and I get this error:

sed: -e expression #1, char 31: unterminated address regex

Basically what I'm doing is using tac to search through a file backwards so that I can locate the preceeding line that starts with a backslash and assign it to the variable remote_file_path.

THank you

  • 1
    Single quotes don't expand variables. Try searching around, there are hundreds of similar questions here. – choroba Nov 01 '18 at 19:18
  • I did put in my post that if I use double quotes it breaks the rest of the command. Then I get this error: sed: -e expression #1, char 31: unterminated address regex – Steve Campbell Nov 01 '18 at 19:19
  • Possible duplicate of [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Cyrus Nov 01 '18 at 20:13

1 Answers1

0

Single quotes don't expand variables. Double quotes do, but if the filename contains a slash (or some other character special to sed), it can break the expression. You can use a different regex delimiter (e.g. \=$file_name= d), but only if you're sure the file name can never contain it (or other special sed characters, e.g. a dot).

Use a real language with variables, variables in shell are just macros; for example, you can use Perl:

f=$file_name perl -ne 'next if 1 .. /\Q$ENV{f}/; print, last if /^\\/'
  • \Q makes all the special characters in $f literal (see quotemeta).
choroba
  • 231,213
  • 25
  • 204
  • 289