echo "11111"| awk 'BEGIN{FS=OFS=""}{$4="x";print}'
linux111x1ide
echo "11111"| awk '{print $0=gensub("1","x",4)}'
工具111x1
spaecho "11111"| awk '{print $0=gensub("1","x","g")}'
unixxxxxx字符串
[解析]get
閣下是否是豁然開朗了不少?這就是gensub的便捷,在這裏面再也不須要字段分割符,用數字便可指定你要替換的某一位。g和G是指全局替換,要雙引號標記起來。特別注意的是gensub是不會修改原記錄的,因此要對$0進行一個賦值。「themodified string is returned as the result of the function and theoriginal target string is not changed」。接下來咱們再看看一個更復雜的運用。string
echo "unix linux"| awk '{print gensub(/(.+)(.+)/,"\\2\\1","g")}'
itlinux unix
ioecho "xaax xbx xxx:xaax xbxxxx"| awk -F:-vOFS=":"'{$2=gensub(/x([^x]+)x/,"\\1YY",2,$2)}1'
xaax xbx xxx:xaax bYYxxx