Replace string variable with string variable using Sed

Solution 1

You need to escape your oldline so that it contains no regex special characters, luckily this can be done with sed.

old_line=$(echo "${old_line}" | sed -e 's/[]$.*[\^]/\\&/g' )
sed -i -e "s/${old_line}/${new_line}/g" ethernet

Solution 2

Since ${old_line} contains many regex special metacharacters like *, ? etc therefore your sed is failing.

Use this awk command instead that uses no regex:

awk -v old="$old_line" -v new="$new_line" 'p=index($0, old) {
      print substr($0, 1, p-1) new substr($0, p+length(old)) }' ethernet
Share:
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin 29 days

Related