0

How to modify a content of particular line in a file using Shell scripting, when the text in that line is repeated many time in other lines as well. For example in the file i have :

##############################
[CC-COARSEGRID-PRECONDITIONER]
##############################
# Relaxation parameter
domega = 1.0
##############
[CC-PRECONDMK]
##############
# Relaxation parameter
domega = 0.9

I want to change the last line to: domega = 0.4

  • 3
    if it is always the last line sed -i -e '$s/=.*$/= 0.4/' file – Archemar Oct 21 '15 at 13:14
2

You could use sed/tac:

tac file | sed '0,/domega = .*/s/domega = .*/domega = 0.4/' | tac
  • tac file reverses the file.
  • 0,/domega = .*/ only do the replacement between the line 0 and the first occurence of domega (is now the last).
  • s/domega = .*/domega = 0.4/ then do the relacement.
  • tac reverses again.

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.