简体   繁体   English

使用sed-shell脚本将值附加到变量

[英]append value to variable using sed - shell script

I am trying to search the file using the key and add 123 to the end of line. 我正在尝试使用密钥搜索文件,并将123添加到行尾。 and other than the key "hello" I dont have values of hello. 除了关键的“你好”我没有你好的价值观。 How can I achieve this sed or awk? 我怎样才能实现这个sed或awk?

file contents 文件内容

hello="80,30,255,64" 你好= “80,30,255,64”

expected output 预期产出

hello="80,30,255,64,123" 你好= “80,30,255,64,123”

try the below sed expression: 尝试下面的sed表达式:

sed 's/"$/,123"/' inputfile

It should give you: 它应该给你:

hello="80,30,255,64,123"

以下awk代码可能会帮助您。

awk -F"\"" '{$2=$2 ",123"} 1' OFS="\""  Input_file

using awk 使用awk

awk 'sub(/"$/,",123&")+1' infile

Test Results: 检测结果:

$ echo 'hello="80,30,255,64"' | awk 'sub(/"$/,",123&")+1'
hello="80,30,255,64,123"

sub(/"$/,",123&")+1 is incase if sub() , does not return non zero, then still print such line sub(/"$/,",123&")+1如果sub() ,不返回非零,则仍然打印这样的行

 I am trying to search the file using the key and add 123 to the end of line. 

Can't find any key here, if you mean to say hello is your key then 在这里找不到任何钥匙,如果你的意思是hello你的钥匙那么

awk '/^hello=/{ sub(/"$/,",123&") }1' infile > outfile

Explanation: 说明:

  • /^hello=/ - look for line starts with hello= /^hello=/ - 查找以hello=开头的行

( ^ indicates the beginning of the string.) ^表示字符串的开头。)

  • sub(/"$/,",123&") - substitute "$ sub(/"$/,",123&") - 替换"$

( $ indicates the end of the string ) $表示字符串的结尾)

with comma, 123 and & 用逗号,123和&

( special character & appears in replacement, it stands for the precise substring that was matched by regexp. ) (特殊字符&出现在替换中,它代表与regexp匹配的精确子字符串。)

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

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