找到模式并在文件中追加和替换它 [英] find patern and append and replace it in file

查看:51
本文介绍了找到模式并在文件中追加和替换它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索一个模式并将其附加到文件中.

I want to search for a pattern and append to that in a file.

准确地说,这里是我的模式的一个例子,一个文件中有数百个.

To be precise here is an example of my pattern and there are hundreds in a file.

         <size>30</size> needs to be replaced with <size>30,-1</size>

但是代替 30 的数字是一个变量(可以是 0-99 之间的任何数字)

But the number in place of 30 is a variable (it can be any number from 0-99)

我是 sed 的新手,但我已经将 sed 用于已知模式,但不知道如何在模式中有变量时进行修改.

I am new to sed but I have used sed with known patterns but dont know how to modify when there is a variable in the pattern.

我还看到了一个示例,其中代码放在 .sed 文件中

also I saw a sample where the code is placed in .sed file

      for example: sed replace.sed datafile.xyz > new.xyz 

我如何做到这一点?

谢谢

推荐答案

使用 sed 你可以用括号存储部分匹配的文本并将其包含在替换中:

With sed you can store part of the matched text with parentheses and include it in the replacement:

http://www.grymoire.com/Unix/Sed.html#呃-4

例如:

sed 's:<size>\([0-9][0-9]*\)</size>:<size>\1,-1</size>:g' ...

这篇关于找到模式并在文件中追加和替换它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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