Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find string and replace line in linux

I'd like to do the following, but I can't seem to find an elegant way to do it.

I have a text file that looks like this:

..more values
stuff = 23   
volume = -15
autostart = 12
more values..

Now the "volume" value may change, depending on the circumstance. I need a script that can find that line of text with "volume = xxx" then replace it with the line "volume = 0". What is an elegant solution to this? This line in the text is not guaranteed to be the first line, so I need a way to find it first.

like image 370
jliu83 Avatar asked Dec 08 '22 20:12

jliu83


2 Answers

sed 's/^volume =.*/volume = 0/g' file.txt
like image 105
Lev Levitsky Avatar answered Feb 16 '23 07:02

Lev Levitsky


With sed you can say:

sed '/^volume/s/.*/volume = 0/' infile
like image 24
Thor Avatar answered Feb 16 '23 07:02

Thor