简体   繁体   English

sed/bash 中的变量替换

[英]Variable substitution in sed/bash

I'm trying to do variable substitution in sed/bash.我正在尝试在 sed/bash 中进行变量替换。

I have seen this example available here , but it does not replace the data as required.我在这里看到了这个例子,但它没有根据需要替换数据。 The value observed in line 20 of $C_CONF is $RAM , when I want it to be 100 .在 $C_CONF 的第 20 行观察到的值是$RAM ,当我希望它是100

Line 20 of $C_CONF is AllowedRAMSpace=SomeValue and I would like to change that value on-the-fly. $C_CONF第 20 $C_CONFAllowedRAMSpace=SomeValue ,我想即时更改该值。

This is the trick I created, but it is a very ugly and inefficient one.这是我创造的技巧,但它非常丑陋且效率低下。

 32 for RAM in 100 #80 60 40 20
 33 do  
 34     sudo sed -i -re 's/(AllowedRAMSpace=)[^=]*$/\1"'"$RAM"'"/' $C_CONF
 35     sudo sed -i '20s/"//' $C_CONF
 36     sudo sed -i '20s/"//' $C_CONF 
 37 done

What is the other way?另一种方式是什么?

尝试使用 awk 代替

awk 'BEGIN{RAM=100}NR==20{print gensub(/(AllowedRAMSpace=)([^=].*$)/,"\\1"RAM,"g", $0)}' filename 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM