[英]append value to variable using sed - shell script
我正在嘗試使用密鑰搜索文件,並將123添加到行尾。 除了關鍵的“你好”我沒有你好的價值觀。 我怎樣才能實現這個sed或awk?
你好= “80,30,255,64”
你好= “80,30,255,64,123”
嘗試下面的sed表達式:
sed 's/"$/,123"/' inputfile
它應該給你:
hello="80,30,255,64,123"
以下awk代碼可能會幫助您。
awk -F"\"" '{$2=$2 ",123"} 1' OFS="\"" Input_file
使用awk
awk 'sub(/"$/,",123&")+1' infile
檢測結果:
$ echo 'hello="80,30,255,64"' | awk 'sub(/"$/,",123&")+1'
hello="80,30,255,64,123"
sub(/"$/,",123&")+1
如果sub()
,不返回非零,則仍然打印這樣的行
I am trying to search the file using the key and add 123 to the end of line.
在這里找不到任何鑰匙,如果你的意思是hello
你的鑰匙那么
awk '/^hello=/{ sub(/"$/,",123&") }1' infile > outfile
說明:
/^hello=/
- 查找以hello=
開頭的行 ( ^
表示字符串的開頭。)
sub(/"$/,",123&")
- 替換"$
( $
表示字符串的結尾)
用逗號,123和&
(特殊字符&
出現在替換中,它代表與regexp匹配的精確子字符串。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.