linuxunixsedstring-substitution

sed substitution is not working with variable


cat file.txt

...Some random string...
...String random string...
service_account_file = $HOME/easyclone/accounts/1.json
...Some random string...
...Some random string...

I just need to substitute the end part of line 3 ( 1 in this case , ie the number before string .json) with the value of a predefined variable

rjc1=999 && sed -i "3s/*\.json/$rjc1\.json/" file.txt

I tried the above command intending to replace 1.json by 999.json but it doesn't seem to work

Expected result with the above variable value

...Some random string...
...String random string...
service_account_file = $HOME/easyclone/accounts/999.json
...Some random string...
...Some random string...

Can anyone please help me to fix this small issue

Note :

  1. i didn't simply substitute 1.json in file.txt because it could be any random number originally in file.txt.
  2. Also didn't used global substitution as i need to substitute value in particular line only

Solution

  • sed -ri "3s/[[:digit:]]+\.json/$rjc1.json/" file
    

    On the third line, substitute, one or more digits and then ".json" for the rjc1 variable followed by ".json"