0

I have for example this line in my file (note that the numbers after = are every time different):

abcd=1234

and I need to change it to:

abcd=9999

How can I do it?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Adam
  • 2,152
  • 4
  • 35
  • 45
  • Man, if anybody help me I will accept his answer, but your answer is not good. I found solution myself just now. I'll post it in my question. – Adam Jul 13 '12 at 21:49
  • I honestly don't see how that is any different from mine based on the criteria you specified. – Drise Jul 13 '12 at 21:53
  • Adam: Feel free to post an actual answer if you have an answer. Please don't copy an answer into the question itself. – Mooing Duck Jul 13 '12 at 21:54
  • Possible duplicate of [Replace whole line containing a string using Sed](https://stackoverflow.com/questions/11245144/replace-whole-line-containing-a-string-using-sed) – tripleee Dec 12 '18 at 06:43

1 Answers1

2

Try sed -i.backup -e 's/abcd=.*/abcd=9999/' filename.txt

Example in filename.txt

abcd=123
abcd=15652

Output as expected:

abcd=9999
abcd=9999

Note: It has been presented to me that this is the GNU sed, which has some extensions apparently that make it different from the normal sed. I was not aware of this, and I have no means to verify this. If someone has the non-gnu solution, please feel free to edit it in.

Drise
  • 4,310
  • 5
  • 41
  • 66
  • I am assuming a lot here, including that you want to change anything labeled `abcd=` to `abcd=9999`. – Drise Jul 13 '12 at 21:40
  • Adam, I literally just ran this. I'm most definitely sure it works. – Drise Jul 13 '12 at 21:52
  • If it worked I'd like to accept your answer, but it really doesn't work. – Adam Jul 13 '12 at 21:57
  • 1
    Can you show how it does not? Maybe a more complete example of what you need processed? – Drise Jul 13 '12 at 21:58
  • Tested and works on OS X 10.7 and Ubuntu 13.04. @Adam make sure you get the spaces right. `-i .backup` is not portable, but `-i.backup` is. – Felix Rabe Sep 21 '13 at 14:58