user2764736
user2764736

Reputation: 29

sed is not working when I use variables

This works if I don't use variables. I don't understand why it won't work when I place variables inside. Can anyone help? The variables contain a path to a file.

sed s/$old/$new/ Current_series_list.txt"

Upvotes: 1

Views: 81

Answers (2)

user1804599
user1804599

Reputation:

Paths may contain forward slashes. Use a different character for separating the command arguments, such as #:

sed "s#$old#$new#" Current_series_list.txt

Of course, this is still fragile due to regex metacharacters. Consider using the following instead:

ruby -e '$stdin.each_line { |l| puts l.sub(ARGV[0], ARGV[1]) }' \
     "$old" "$new" < Current_series_list.txt

A more portable equivalent in Perl is left as an exercise for the reader.

Upvotes: 3

Shaggy Frog
Shaggy Frog

Reputation: 27601

What is the value of old and new? If they contain slashes, then they will expand the sed pattern to something that isn't valid. Your trailing quotation mark probably is also not helping.

Upvotes: 0

Related Questions