【版权声明:转载请保留出处:blog.csdn.net/gentleliu。Mail:shallnew at 163 dot com】
这一节来见识一下a w k许多强大的字符串函数及其使用方法。
1、 sub 和 gsub函数:# awk 'BEGIN{buf="Hello,awk! there is awktutorial!";sub(/awk/, "world", buf); print buf}' Hello,world! there is awk tutorial! # echo "Hello,awk! there is awk tutorial" | awk'{sub(/awk/, "world"); print $0}' Hello,world! there is awk tutorial # awk 'BEGIN{buf="Hello,awk! there is awktutorial!";gsub(/awk/, "world", buf); print buf}' Hello,world! there is world tutorial! # echo "Hello,awk! there is awk tutorial" | awk'{gsub(/awk/, "world"); print $0}' Hello,world! there is world tutorial #2、 index函数
# echo "Hello,awk! there is awk tutorial" | awk '{printindex($0, "awk")}' 7 #3、 length函数,blength函数
# echo"Hello,awk! there is awk tutorial" | awk '{print length()}' 32 #4、 substr函数
# echo"Hello,awk! there is awk tutorial" | awk '{print substr($0, 5)}' o,awk! there isawk tutorial # echo"Hello,awk! there is awk tutorial" | awk '{print substr($0, 5, 6)}' o,awk! #5、 spilt函数
# echo"Hello,awk! there is awk tutorial" | awk '{split($0, arr); for (i inarr){print i,arr[i]}}' 4 awk 5 tutorial 1 Hello,awk! 2 there 3 isawk for …in 循环,是一个无序的循环。并不是从数组下标1…n ,因此使用时候需要注意。
# awk 'BEGIN{system("echo $HOME")}' /root原来awk里面还可以调用shell 命令,高大上啊。
原文地址:http://blog.csdn.net/shallnet/article/details/38925317