开发者

Sed for partial replacement?

开发者 https://www.devze.com 2023-03-21 22:56 出处:网络
Imagine I have a file that has the following type of line: FIXED_DATA1 VARIABLE_DATA FIXED_DATA2 I want to change the fixed data and leave the variable data as is. For various reasons, using two sed

Imagine I have a file that has the following type of line:

FIXED_DATA1 VARIABLE_DATA FIXED_DATA2

I want to change the fixed data and leave the variable data as is. For various reasons, using two sed operations to replace the fixed data will not work. For instance, the fixed fields might be double-quotes, and the line has other areas containing them, thus really the regex is written to match a pattern in the variable data and the fixed data.

If I'm bent on using sed, is there a way to change both fixed da开发者_如何转开发ta fields at once while leaving the variable field unchanged?

Thanks.


You need to partition the line into the three pieces, replace the outer two and leave the middle alone:

sed 's/^FIX1 \(.*\) FIX2$/New \1 End/'

You can make the beginning and end matches more complex as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号