测试环境:
[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 ~!@#$%^*((
学习:
下一步重点学习(正恻表达式就稍微复习一下就可以,自己已经比较熟悉了):
1>. 使用awk作文本处理的数值计算(awk的运算符)
1>. 在awk中使用脚本,多行复杂的awk书写(变量定义,if使用,循环和数组的使用,系统函数的使用,,awk内部的环境变量)
2>. 在不同平台awk的区别,怎么写出通用的awk(linux,aix, hpux, sunos)
----------------
添加点东西:
输出'符号,从cu上看到的:
[gan@localhost log]$ awk 'begin {print "'\''"}'
' |
----------------
阅读(8049) | 评论(1) | 转发(0) |