skip to Main Content

I want to remove ${} among arithmetic form in the file using sed
for example abc=$(( ${var}+3 )) to abc=$(( var+3 ))

I’m using positional swapping in sed
something like

sed -E 's/(w+W$(( ) (${) (w+) (}) (.*)/1 3 5/g file.txt'

but it extracts only abc=3 when I use

echo abc=$((( ${var}+3 )) | sed -E 's/(w+W$(( ) (${) (w+) (}) (.*)/1 3 5/' 

in terminal, just to check if it works all right

and it did nothing on shell script how can I remove only ${} part of the file?

I am using Mac OS and also tried on Ubuntu but it was still the same

3

Answers


  1. Using sed

    $ sed -E 's/${([^}]*)}/1/' input_file
    abc=$(( var+3 ))
    
    Login or Signup to reply.
  2. 1st solution: Using capturing groups concept here and substituting matched values with captured values and only with which are required please try following sed code. Here is the Online Demo for used regex in sed code.

    s='abc=$(( ${var}+3 ))' ##shell variable
    sed -E 's/^([^=]*=$(( )${([^}]*)}(.*)$/123/' <<<"$s"
    

    OR use following in case you have an Input_file from where you want to substitute values.

    sed -E 's/^([^=]*=$(( )${([^}]*)}(.*)$/123/' Input_file
    


    2nd solution: Using Perl’s one-liner approach using Lazy match regex to make life easier here, one could try following.

    perl -pe 's/^(.*?)${(.*?)}(.*)$/$1$2$3/' Input_file
    
    Login or Signup to reply.
  3. printf="abc=$(( ${var}+3 )) to abc=$(( var+3 ))" | tr -d '{' | tr -d '}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search