[英]replace string with variable on file
I have files with several lines like: 我有几行内容,例如:
) extent size 256 next size 512 lock mode row;
) extent size 512 next size 512 lock mode row;
) extent size 256 next size 512 lock mode row;
) extent size 512 next size 512 lock mode row;
) extent size 256 next size 512 lock mode row;
) extent size 512 next size 512 lock mode row;
) extent size 512000 next size 48 lock mode row;
) extent size 512 next size 512 lock mode row;
I would like to change all this lines to: 我想将所有这些行更改为:
) extent size 16 next size 16 lock mode row;
I can get all with: 我可以用:
cat file | grep "extent size"
But I don't know how to change variable text 但我不知道如何更改可变文字
Thanks for any help, 谢谢你的帮助,
SP SP
You want to replace all digit chunks with some other number on lines that contain extent size
. 您想用包含扩展区
extent size
行extent size
其他一些数字替换所有数字块。
You may use the following sed
solution: 您可以使用以下
sed
解决方案:
val=16;
sed "/extent size/{s/[0-9]\{1,\}/$val/g}" file > newfile
Details 细节
/extent size/
- finds lines that contain this text /extent size/
-查找包含此文本的行 {s/[0-9]\\{1,\\}/$val/g}
- on the found line, replaces one or more digits with the val
contents (assuming it will be a simple number, no additional processing is required). {s/[0-9]\\{1,\\}/$val/g}
-在找到的行上,将一个或多个数字替换为val
内容(假设它将是一个简单的数字,不需要其他处理) 。 See the online sed
demo . 请参阅在线
sed
演示 。
If comfortable with awk solution. 如果对awk解决方案感到满意。 Try this:-
尝试这个:-
grep "extent size" filename | awk '$4=$7="16"'
Explanation 说明
grep "extent size" => Grep the line contain words **"extent size"**
awk '$4=$7="16"' => change the value of **column 4 and 7** to 16.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.