本文介绍了在linux中查找字符串并替换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做以下事情,但我似乎无法找到一个优雅的方式来做到这一点。



我有一个文本文件,看起来像这个:

$ p $ ..更多值
stuff = 23
volume = -15
自动启动= 12
more values ..

现在音量值可能会改变,具体取决于情况。我需要一个脚本,可以找到volume = xxx的文本行,然后用volume = 0行替换它。什么是这个优雅的解决方案?本文中的这一行不保证是第一行,所以我需要一种方法来首先找到它。

解决方案
  sed's / ^ volume =。* / volume = 0 / g'file.txt 


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.

解决方案
sed 's/^volume =.*/volume = 0/g' file.txt

这篇关于在linux中查找字符串并替换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:23