awk 使用的情形
- 直接在命令行下使用
如某文件为test.txt
现在在使用awk打印空行
可以这样
cat test.txt
文件内容如下
hello world
hello world
hello world
hello world
hello world
hello world
world
world
world
xxxddsdad
总用量 24
-rwxrwxr-x 1 oc oc 114 Apr 11 01:09 expcect_test.sh
-rw-rw-r-- 1 oc oc 6 Apr 12 20:27 file1
-rw-rw-r-- 1 oc oc 6 Apr 12 20:27 file2
-rw-rw-r-- 1 oc oc 206 Apr 11 00:35 read.sh
-rw-rw-r-- 1 oc oc 102 Apr 13 21:38 test.txt
-rw-rw-r-- 1 oc oc 112 Apr 11 01:17 until_test.sh
awk '/^$/{print "this is a blank line"}' test.txt
this is a blank line
this is a blank line
- 将awk写入文件,再使用awk调用某文件
cat scr.awk
#! /bin/awk -f
/^$/{print "this is a blank line"}
awk -f scr.awk test.txt
或者加执行权限
chmod a+x ./scr.awk
./scr.awk test.txt
awk 记录和域
awk认为文件为结构化的,以行为记录,每行的字符串为域,域间用空格或者tab键等进行分割,这些分割域的符号为分割符
比如 有一个名为test.txt的文件,其内容是
先使用awk打印全部域,使用$0
先打印第1列和第4列,使用
1和
4
指定域分割符,使用-F
先更改文件test.txt,在每行test2后增加一个tab
可以看出以tab键分割的域1为test1 和test2
域2 为test3和test4