桑达模式匹配和替换 [英] Sed pattern to match and replace
问题描述
我怎么能写一个sed前pression,看起来通过线路来匹配
How can I write a sed expression, which looks through a line to match
ID INT
和变更
ID字符串
这将是该行唯一的内容(没有什么比其他ID INT),并应不会改变其他行。
this will be the only content in that line (nothing other than, Id int) and should not alter other lines.
我试图沿东西线
find . -type f -print0 | xargs -0 sed -i '' 's~^\([[:space:]]*\)"Id"[[:space:]]*"int"[:space:]]*$~\1"Id string"~g'
但章EXP是错误的。
But the reg exp is wrong.
感谢。
要进一步明确:
有空间(不一致)编号
,在标识之间
和 INT
。因此,需要匹配[:空间:]] *,而不是标签或前pressions像 S / ^ ID INT $
To clarify further:
There are spaces (inconsistent) before Id
, between Id
and int
. So, needs to match [[:space:]]* , instead of tab or expressions like s/^Id int$
所以以匹配线可以是
Id int
Id int
Id int
它可以被替换
Id string
Id string
Id string
手段,无须preserve的空格数
means, no need to preserve the number of spaces
推荐答案
更容易使用awk:
Easier with awk:
awk '$2=="int"{$2="string"}1'
和可能更容易调整到您的实际需求。
And potentially easier to tweak to your actual needs.
这篇关于桑达模式匹配和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!