如何使用sed仅在包含另一个图案线来替换在文件中的图案 [英] How to use sed to replace a pattern in a file only in lines that contain another pattern
问题描述
在我的具体情况,我需要遍历包含存储URL的变量几百个文件。所有网址都不同,但对于这个例子来说,我们可以假设他们与所有终端COM
In my specific situation, I need to loop through several hundred files that contain a variable which stores a URL. All of the URLs are different, but for the sake of this example we can assume they all end with 'com"'
我需要添加一些文字,立即COM以下
I need to add some text immediately following the com
由于所有的URL都会有不同的领域,我不能用一个sed命令,如:
Because all of the URLs will have different domains I cannot use a sed command like:
SED -iS / ^环境=someurl.com/环境=someurl.com.origin.net/ G'
由于该文件包含其中可能包含COM其他行,我不能用这样的:
Because the files contain other lines which may contain com, I cannot use something like:
SED -i的/ COM / com.origin.net / G'
我需要这样做的搜索,只有在包含模式,如下面的正则表达式我的例子中的某一行替换:
I need to do this search and replace only on a specific line containing a pattern, such as from my example below in regex:
^环境。*
如果我能集中使用的sed只匹配上面的图案,然后我可以使用的sed如SED -i 的/ COM / com.origin.net /一些改编行G'
。当然,我可以写一个过于复杂的bash脚本来做到这一点,但我知道有一个很好的单行方式告诉SED找到一个模式,仅在图案中发现的线上工作,以及搜索和替换另一种模式有一个新的字符串。
If I could focus the use of sed only to the line that matched the above pattern then I could use some adaptation of sed such as sed -i 's/com/com.origin.net/g'
. I could of course write an overly complicated bash script to do this, but I know there is a nice one liner way to tell sed to find a pattern, only work on the line that the pattern is found in, and search and replace another pattern with a new string.
在谷歌搜索中,只是半相关的链接,我发现这个是:
<一个href=\"http://unix.stackexchange.com/questions/37518/how-to-sed-only-that-lines-that-contains-given-string\">http://unix.stackexchange.com/questions/37518/how-to-sed-only-that-lines-that-contains-given-string
In searching Google, the only semi relevant link I found about this was:
http://unix.stackexchange.com/questions/37518/how-to-sed-only-that-lines-that-contains-given-string
但它没有充分回答我的问题。也能够约束的sed匹配一个很好的工具将模式添加到我的sed阿森纳一般只是一条线。
but it didn't sufficiently answer my question. Also being able to constrain sed in general to just a line that matches a pattern would by a nice tool to add to me sed arsenal.
任何帮助,导致这里的解决方案将是非常美联社preciated。
Any help that leads to solution here will be very appreciated.
下面是一个文件的一个例子
Here is an example of a file
###示例文件
#
#
#someurl.com
环境=someurl.com
### Example File
#
#
# someurl.com
ENVIRONMENT="someurl.com"
我要替换的环境=someurl.com'环境=someurl.com.orign.net
I want to replace 'ENVIRONMENT="someurl.com"' with 'ENVIRONMENT="someurl.com.orign.net"'
我可能会插入来自这个职位在同一行的shell脚本来源,如sed命令:
I would probably insert the sed command derived from this post in one line shell script such as:
`中的* .config F; DO File2BeEdited = $(猫$ F | grep的^ PatternToIdentifyLine | grep的-v RemoveLinesThatAreAlreadyCorrect);如果[$ File2BeEdited=!;然后RunSedCommand; printf的\\ N实测值需要被编辑的文件\\ N运行的sed在$ F >> log.log。做
`for f in *.config; do File2BeEdited=$(cat $f | grep ^PatternToIdentifyLine | grep -v RemoveLinesThatAreAlreadyCorrect); if [ "$File2BeEdited" != "" ];then RunSedCommand; printf "\nFound a file that needs to be edited.\nRunning sed on $f >> log.log"; done
所以,我可以遍历目录中的每个配置文件,并选择性地进行更改
So I could loop through every configuration file in a directory and selectively make this change
推荐答案
下面的命令行显示了两种可能的解决方案:可以限制更换某些线路与 /正则表达式/
。你也可以使用替代与匹配的文本\\ 0
, \\ 1
等
The following command line shows two possible solutions: You can restrict the replacement to certain lines with /regex/
. And you can use the matched text in replacements with \0
, \1
, etc.
sed -e '/^\(ENVIRONMENT="[^"]*\.com\)"/s//\1.origin.net"/'
这篇关于如何使用sed仅在包含另一个图案线来替换在文件中的图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!