sed/awk advance

sed  

$ echo test.file

[Group1]
cacheways = 19

[Group2]
cacheways = 19

$ ls test.sh

1 FILE=test.file
2 # Set Group1 cacheways = 1 3 sed -i -e '/\[Group1\]*/{$!{N;s/\(cacheways = \)\(.*\)/\11/}}' $FILE 4 # Set Group2 cacheways = 10 5 sed -i -e '/\[Group2\]*/{$!{N;s/\(cacheways = \)\(.*\)/\110/}}' $FILE

$ test.sh 

 

ref:

【1】 sed疑难杂症:关于shell变量替换

 使用ASCII码(google search “how to use ascii in sed”)

5.8.1 Escaping Precedence  

$ echo 'a^c' | sed 's/\^/b/'
abc

$ echo 'a^c' | sed 's/\\\x5e/b/'
a^c

更多例子

sed 's/\o100/hell/' <<< 'aaa@bbb'
sed 's/\x40/hell/' <<< 'aaa@bbb'
sed 's/\d64/hell/' <<< 'aaa@bbb'

sed Find and Replace ASCII Control Codes / Nonprintable Characters  

sed 's/'`echo "\d33"`'/foo/g' <<< 'aaa!bbb'

sed -e 's/'$(echo "\d33")'/ESC/g' <<< 'aaa!bbb'

awk

$ ip route get 1
1.0.0.0 via 10.0.0.1 dev ens3 src 10.0.0.14 uid 1000
    cache

$ ip route get 1 | awk '{match($0, /.+src\s([.0-9]+)/, a);print a[1];exit}'
10.0.0.14

awk ip 正则表达式

awk match 用法

awk之match函数

 awk正则表达式捕获

awk内置函数sub gensub gsub match等介绍

linux awk之gensub()函数详解

posted @ 2017-09-10 08:26  lvmxh  阅读(133)  评论(0编辑  收藏  举报