awk --- print学习

测试环境:

[gan@localhost tmp]$ uname -a
Linux 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 version
GNU 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.txt
LINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
[gan@localhost tmp]$ awk '{ print }' file.txt
LINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((

输出文件的一部分:
[gan@localhost tmp]$ a wk '{ print $1}' file.txt
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
[gan@localhost tmp]$ awk '{ print $2 }' file.txt
Date
abc
7823
s0df
8&32*
~!@#$%^*((

增加部分内容输出:
[gan@localhost tmp]$ awk '{ print "header " $1 "\t" $2 }' file.txt
header 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.txt
header
header
header
header
header
header
[gan@localhost tmp]$ awk -F":" '{ print $2 }' dfile.txt
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
[gan@localhost tmp]$ awk -F":" '{ print $2 " " $1 }' dfile.txt
LINE1    header
LINE2    header
LINE3    header
LINE4    header
LINE5    header
LINE6    header

输出包含(不包含)特定字符的行(sed也可以完成该功能):
[gan@localhost tmp]$ awk '/[a-c]/ { print }' file.txt
LINE1 Date
LINE2 abc
[gan@localhost tmp]$ awk '!/[a-c]/ { print }' file.txt
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((

采用判断来输出特定的列数据:
[gan@localhost tmp]$ awk '$1 == "LINE2" { print $2 }' file.txt
abc
http://gan.cublog.cn
部分包含,不包含指定的字符:
[gan@localhost tmp]$ awk '$2 ~ /[a-d]/ { print }' file.txt
LINE1 Date
LINE2 abc
LINE4 s0df
[gan@localhost tmp]$ awk '$2 !~ /[a-d]/ { print }' file.txt
LINE3 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上看到的:http://bbs.chinaunix.net/viewthread.php?tid=15773&extra=&page=2

[gan@localhost log]$ awk 'BEGIN {print "'\''"}'

'

 

----------------

 

你可能感兴趣的:(linux,PHP,IBM,AIX,bbs)