使用grep和sed将一个字符串替换为另一个字符串 [英] Using grep and sed to replace one string with another

查看:363
本文介绍了使用grep和sed将一个字符串替换为另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个文件中的字符串替换一个文件中的一个字符串。虽然我对这些命令没有经验,但我希望grep和sed的组合能够做到最好。

是什么让这更复杂一点是我不知道这两个字符串是什么(我试图自动替换我的文档中的版本号)。我知道在这两种情况下,我在寻找的字符串(比如2.3.4)前面都有version:。

所以我可以说'look在版本:(让我们称之为string1)之后为单词(或者其他可能的行)或者其他可能的文件(在string1中),然后在另一个文件中执行相同的操作(给出string2),并用string2替换string1。



以下是一些示例文本文件: b
$ b file1.txt


这是一个包含

更新版本号的文件。

版本:2.3.4

这里是一个包含更多信息的字符串




file2.txt
$ b


这是一个配置文件<
它可以包含一个旧的版本号

版本:2.3.2

请更新这个


因此,file2.txt的预期输出将变为:

file2.txt

< blockquote>

这是一个配置文件

它可以继续一个旧版本号

版本:2.3.4

请更新这个


谢谢

解决方案

假设您有一个 sed ,它支持选项,

  sed -i's / version:。* / version :1.2.3 /'file1 file2 file3 ... 

您可能想要调整正则表达式通配符; 。* 通过行尾匹配,而 [。0-9] * 匹配最长的可能序列点和数字。您可能还想允许周围空白的变化...但是,因为这可能是本网站上10%的常见问题解答之一,请在此处查找类似问题。



从file1中获取替换字符串并将其应用于file2,file3等,类似于

  new = $( sed -n's / version:// p'file1)
#为了展开$ new
sed -is / version:[.0-9],使用双引号,而不是单引号。 * / version:$ new /file2 file3 ...

第一个 sed 调用将只打印找到并删除version:的行(用空字符串替换)。据推测,文件中只会有这样一行。将输出管道输出到 head -n 1 uniq 或其他东西,或者找到/创建更详细的 sed 脚本。



您通常在文字字符串周围使用单引号,但由于您不需要文字 $ new 在替换中,我们使用双引号,它允许shell在引用的字符串中执行变量替换(以及其他一些替换,我们不在此处介绍)。


I'd like to replace one string from one file with a string from another file. Though I'm not experienced with these commands, I expect some combination of grep and sed would do it best.

What makes this a bit more complicated is that I don't know what either string is (I'm trying to automate replacing the version number on my documentation). I do know that in both cases the string I'm looking for (say "2.3.4") is preceded by "version:"

So can I say 'look for word (or rest of line or whatever is possible) after "version:" (let's call it string1) and do the same in another file (giving string2) and replace string string1 with string2.

Here are some example text files:

file1.txt

This is a file containing
the updated version number.
version: 2.3.4
here is a string with more info

file2.txt

This is a configuration file
It could contain an old version number
version: 2.3.2
Please update this

So the expected output for file2.txt would become:

file2.txt

This is a configuration file
It could contain an old version number
version: 2.3.4
Please update this

Thanks

解决方案

Provided you have a sed which supports the -i option,

sed -i 's/version: .*/version: 1.2.3/' file1 file2 file3 ...

You may want to tweak the regex wildcard; .* matches through the end of the line, whereas [.0-9]* matches the longest possible sequence of dots and digits. You might also want to permit for variations in surrounding whitespace ... But since this is probably among the top 10% FAQs on this site, go look for similar questions at this point.

To obtain the replacement string from file1 and apply it to file2, file3, etc, something like

new=$(sed -n 's/version: //p' file1)
# Use double quotes, not single, in order to expand $new
sed -i "s/version: [.0-9]*/version: $new/" file2 file3 ...

The first sed invocation will only print lines on which "version: " was found and removed (replaced with an empty string). Presumably there will only be one such line in the file. Pipe the output to head -n 1 or uniq or something, or find / create a more elaborate sed script.

You normally use single quotes around literal strings, but since you don't want a literal $new in the replacement, we use double quotes, which allow the shell to perform variable replacement (and a number of other substitutions we don't go into here) in the quoted string.

这篇关于使用grep和sed将一个字符串替换为另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆