Conf.txt
Enable_FUN Yes
FUN_Privilege_Level User
Force_FUN_Payload_Authentication No
Force_FUN_Payload_Encryption No
Character_Accumulate_Interval 12
Character_Send_Threshold 96
FUN_Retry_Count 7
FUN_Retry_Interval 50
Non_Volatile_Bit_Rate 115200
Volatile_Bit_Rate 115200
我需要将“Volatile_Bit_Rate”值从115200更新为38400 注意:Volatile_Bit_Rate和Non_Volatile_Bit_Rate具有相同的匹配字符串,格式(空格/制表符)也应保持不变。
我正在尝试以下方式,这是有效的。
var=38400
sed -i "s/\bVolatile_Bit_Rate\b/Volatile_Bit_Rate $var/" solconf
然后我需要删除字符串的结尾。
但是,任何人都可以通过以下方式向我推荐正则表达式,这将有所帮助。
sed -i "s/\bVolatile_Bit_Rate\b(expression for allspaces)(somenumbers) /Volatile_Bit_Rate(spaces)$var/" solconf
答案 0 :(得分:5)
你可以这样做:
sed 's/\(^Volatile_Bit_Rate[[:blank:]]\+\)[0-9]\+$/\138400/' file.txt
要编辑文件:
sed -i 's/\(^Volatile_Bit_Rate[[:blank:]]\+\)[0-9]\+$/\138400/' file.txt
示例:强>
$ cat file.txt
Enable_FUN Yes
FUN_Privilege_Level User
Force_FUN_Payload_Authentication No
Force_FUN_Payload_Encryption No
Character_Accumulate_Interval 12
Character_Send_Threshold 96
FUN_Retry_Count 7
FUN_Retry_Interval 50
Non_Volatile_Bit_Rate 115200
Volatile_Bit_Rate 115200
$ sed 's/\(^Volatile_Bit_Rate[[:blank:]]\+\)[0-9]\+$/\138400/' file.txt
Enable_FUN Yes
FUN_Privilege_Level User
Force_FUN_Payload_Authentication No
Force_FUN_Payload_Encryption No
Character_Accumulate_Interval 12
Character_Send_Threshold 96
FUN_Retry_Count 7
FUN_Retry_Interval 50
Non_Volatile_Bit_Rate 115200
Volatile_Bit_Rate 38400
答案 1 :(得分:2)
sed -ri "s/^(Volatile_Bit_Rate\s*)[0-9]+$/\1$var/g" file
答案 2 :(得分:0)
我找到了上述要求的一个简单答案,
sed" / \ bVolatile_Bit_Rate \ b / s / \ b38400 \ b / 115200 / g" CONF
答案 3 :(得分:0)
另一种变体:
var=38400
sed -i "/\bVolatile_Bit_Rate\b/s/[0-9]* *\$/$var/" solconf