测试环境:
[gan@localhost tmp]$
uname -aLinux localhost.localdomain 2.6.25.4-10.fc8 #1 SMP Thu May 22 23:34:09 EDT 2008 i686 i686 i386 GNU/Linux
http://gan.cublog.cn[gan@localhost tmp]$
awk -W versionGNU Awk 3.1.5
......
1>. 简单字符转换输出
[gan@localhost tmp]$ cat file.txt
LINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
http://gan.cublog.cn输出整个文件:
[gan@localhost tmp]$
awk '{ print $0 }' file.txtLINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
[gan@localhost tmp]$
awk '{ print }' file.txtLINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
输出文件的一部分:
[gan@localhost tmp]$ a
wk '{ print $1}' file.txtLINE1LINE2
LINE3
LINE4
LINE5
LINE6
[gan@localhost tmp]$
awk '{ print $2 }' file.txtDate
abc
7823
s0df
8&32*
~!@#$%^*((
增加部分内容输出:
[gan@localhost tmp]$
awk '{ print "header " $1 "\t" $2 }' file.txtheader LINE1 Date
header LINE2 abc
header LINE3 7823
header LINE4 s0df
header LINE5 8&32*
header LINE6 ~!@#$%^*((
http://gan.cublog.cn使用指定的分隔符号来取数据:
[gan@localhost tmp]$ cat dfile.txt
header :LINE1 :Date
header :LINE2 :abc
header :LINE3 :7823
header :LINE4 :s0df
header :LINE5 :8&32*
header :LINE6 :~!@#$%^*((
[gan@localhost tmp]$
awk -F":" '{ print $1 }' dfile.txtheader
header
header
header
header
header
[gan@localhost tmp]$
awk -F":" '{ print $2 }' dfile.txtLINE1
LINE2
LINE3
LINE4
LINE5
LINE6
[gan@localhost tmp]$
awk -F":" '{ print $2 " " $1 }' dfile.txtLINE1 header
LINE2 header
LINE3 header
LINE4 header
LINE5 header
LINE6 header
输出包含(不包含)特定字符的行(sed也可以完成该功能):
[gan@localhost tmp]$
awk '/[a-c]/ { print }' file.txtLINE1 Date
LINE2 abc
[gan@localhost tmp]$
awk '!/[a-c]/ { print }' file.txtLINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
采用判断来输出特定的列数据:
[gan@localhost tmp]$
awk '$1 == "LINE2" { print $2 }' file.txtabc
http://gan.cublog.cn部分包含,不包含指定的字符:
[gan@localhost tmp]$
awk '$2 ~ /[a-d]/ { print }' file.txtLINE1 Date
LINE2 abc
LINE4 s0df
[gan@localhost tmp]$
awk '$2 !~ /[a-d]/ { print }' file.txtLINE3 7823
LINE5 8&32*
LINE6 ~!@#$%^*((
学习:
http://www.ibm.com/developerworks/cn/linux/shell/awk/awk-1/下一步重点学习(正恻表达式就稍微复习一下就可以,自己已经比较熟悉了):
1>. 使用awk作文本处理的数值计算(awk的运算符)
1>. 在awk中使用脚本,多行复杂的awk书写(变量定义,if使用,循环和数组的使用,系统函数的使用,,awk内部的环境变量)
2>. 在不同平台awk的区别,怎么写出通用的awk(Linux,AIX, HPUX, SunOS)
----------------
添加点东西:
输出'符号,从CU上看到的:
[gan@localhost log]$ awk 'BEGIN {print "'\''"}'
' |
----------------
阅读(8089) | 评论(1) | 转发(0) |