From the below file I have to replace "100" with a value stored in variable.


dropNumber = 100
patchNumber =
randomNumber = 10000
versionCode = dropNumber + patchNumber

Tried few options which failed: 1)

OLD_DROP_NUMBER=$(cat file.txt | grep dropNumber | head -1 | sed 's/^.*= //')
echo "Drop Number: $NEW_DROP_NUMBER" | awk -v srch="$OLD_DROP_NUMBER" -v repl="$NEW_DROP_NUMBER" '{ sub(srch,repl,$0); print $0 }' file.txt > file.txt &&  mv properties.tmp file.txt

This approach is replacing value of randomNumber too which is not intended.


sed -i -e 's/$OLD_DROP_NUMBER/$NEW_DROP_NUMBER/g' file.txt 

This returns error "sed: s/100/200/g: No such file or directory"

Can somebody please help in designing the command to replace only expected value?

  • 2
    Are you sure that's the exact sed command you ran? That error message means that sed thinks s/100/200/g is a filename to read from, which is not what the -e option does. Additionally, the variables ($OLD_DROP_NUMBER etc) wouldn't get expanded inside single quotes.
    – JigglyNaga
    Oct 17, 2019 at 11:53
  • Are you using macOS?
    – Kusalananda
    Oct 17, 2019 at 11:56

1 Answer 1


Awk programs consist of pattern {action} pairs: if you don't supply a pattern, then action will be applied to every record. In your case, you can add a pattern to match only records (lines) that begin with dropNumber:

awk ... '/^dropNumber/ { sub(srch,repl,$0) } { print }' ...

Similarly with Sed, use a regular expression to address only the desired line:

sed "/^dropNumber/s/$OLD_DROP_NUMBER/$NEW_DROP_NUMBER/"

(note that you will need double quotes in place of the single quotes shown in your question, else the shell variables will be treated as string literals).

  • Thanks.. addressing the only the desired line by using ^dropNumber worked like charm.
    – Shruti
    Oct 18, 2019 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.