您的位置:首页 > 运维架构 > Shell

shell 实练(1)-awk

2012-11-01 09:56 162 查看
最近基本把shell 脚本编写的内容学习了一遍,看脚本轻松了,但是实际上手还有一定的困难,所以决定找点例子来实练,毕竟有句话说的很好,“纸上得来终觉浅,绝知此事要躬行”;很有道理,最近深刻体会到了!通过实练也算是《shell知识点补充》板块的进阶吧!攻克shell,再说python!!

1、awk

awk 用法:awk ' pattern {action} '

awk “/关键字/” 文件名

Loong:/home/yee/shell# awk '/root/' xaa
显示xaa文件中带root的行

root:x:0:0:root:/root:/bin/bash

Loong:/home/yee/shell# awk '/3/' xaa 显示xaa文件中带3的行

sys:x:3:3:sys:/dev:/bin/sh

sync:x:4:65534:sync:/bin:/bin/sync

proxy:x:13:13:proxy:/bin:/bin/sh

www-data:x:33:33:www-data:/var/www:/bin/sh

backup:x:34:34:backup:/var/backups:/bin/sh

Loong:/home/yee/shell# cat last.txt 用例文件内容

root pts/6 172.16.7.94 Wed Oct 31 10:09 still logged in

yee pts/5 172.16.7.97 Wed Oct 31 10:09 still logged in

yee pts/1 172.16.7.95 Wed Oct 31 10:06 still logged in

root pts/6 172.16.7.94 Tue Oct 30 14:52 - 19:38 (04:45)

yee pts/5 172.16.3.89 Tue Oct 30 08:36 - 19:43 (11:06)

yee pts/1 172.16.7.94 Tue Oct 30 08:34 - 19:38 (11:04)

loongson pts/6 172.16.7.94 Mon Oct 29 15:12 - 19:23 (04:11)

yee pts/5 172.16.3.89 Mon Oct 29 08:59 - 19:08 (10:08)

loongson pts/1 172.16.7.94 Mon Oct 29 08:53 - 19:24 (10:31)

yee pts/5 172.16.7.94 Fri Oct 26 10:53 - 17:27 (06:33)

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell# awk '$1 == "yee"' last.txt 显示第一个变量为yee的行,注意必须带双引号

yee pts/5 172.16.7.97 Wed Oct 31 10:09 still logged in

yee pts/1 172.16.7.95 Wed Oct 31 10:06 still logged in

yee pts/5 172.16.3.89 Tue Oct 30 08:36 - 19:43 (11:06)

yee pts/1 172.16.7.94 Tue Oct 30 08:34 - 19:38 (11:04)

yee pts/5 172.16.3.89 Mon Oct 29 08:59 - 19:08 (10:08)

yee pts/5 172.16.7.94 Fri Oct 26 10:53 - 17:27 (06:33)

Loong:/home/yee/shell# awk '$1 > $2' last.txt 显示出字符串第一个大于第二个的行;

root pts/6 172.16.7.94 Wed Oct 31 10:09 still logged in

yee pts/5 172.16.7.97 Wed Oct 31 10:09 still logged in

yee pts/1 172.16.7.95 Wed Oct 31 10:06 still logged in

root pts/6 172.16.7.94 Tue Oct 30 14:52 - 19:38 (04:45)

yee pts/5 172.16.3.89 Tue Oct 30 08:36 - 19:43 (11:06)

yee pts/1 172.16.7.94 Tue Oct 30 08:34 - 19:38 (11:04)

yee pts/5 172.16.3.89 Mon Oct 29 08:59 - 19:08 (10:08)

yee pts/5 172.16.7.94 Fri Oct 26 10:53 - 17:27 (06:33)

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell# awk '{print NR,NF,$1,$NF}' last.txt 显示文件的当前记录行号、域数(空格隔开也是一个)和每一行的第一个和最后一个域(字符串)

1 10 root in

2 10 yee in

3 10 yee in

4 10 root (04:45)

5 10 yee (11:06)

6 10 yee (11:04)

7 10 loongson (04:11)

8 10 yee (10:08)

9 10 loongson (10:31)

10 10 yee (06:33)

11 0

12 7 wtmp 2012

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk '/loongson/ {print $1,$2+23}' last.txt 显示文件f匹配行的第一域、第二个域加23,不清楚为什么被覆盖了?

loongson 23

loongson 23

Loong:/home/yee/shell# awk '/loongson/ {print $1,$2}' last.txt

loongson pts/6

loongson pts/1

Loong:/home/yee/shell# awk '/loongson/ {print $1 $2}' last.txt

loongsonpts/6

loongsonpts/1

Loong:/home/yee/shell# awk '/loongson/ {print $1$2}' last.txt 逗号,表示显示的两个域有空格隔开

loongsonpts/6

loongsonpts/1

Loong:/home/yee/shell#

Loong:/home/yee/shell# df

文件系统 1K-块 已用 可用 已用% 挂载点

/dev/sda5 20641788 7509808 12083340 39% /

tmpfs 253472 16 253456 1% /lib/init/rw

udev 253472 656 252816 1% /dev

tmpfs 253472 16 253456 1% /dev/shm

/dev/sda6 126849780 2437976 117968136 3% /home

shm 253472 253472 0 100% /tmp

Loong:/home/yee/shell# df | awk '$4>10000000' 通过管道符获得输入,如:显示第4个域满足条件的行

文件系统 1K-块 已用 可用 已用% 挂载点

/dev/sda5 20641788 7509808 12083340 39% /

/dev/sda6 126849780 2437976 117968136 3% /home

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk -F "/" '{print $1}' last.txt 按照新的分隔符“/”进行输出第一个自定义的域

root pts

yee pts

yee pts

root pts

yee pts

yee pts

loongson pts

yee pts

loongson pts

yee pts

wtmp begins Wed Oct 10 09:34:03 2012 没有找到 / ,全部当做第一个域输出

Loong:/home/yee/shell# awk 'BEGIN {FS="/"}{print $1}' last.txt 按照新的分隔符“/”进行输出第一个自定义的域,方法二通过设置输入分隔符

root pts

yee pts

yee pts

root pts

yee pts

yee pts

loongson pts

yee pts

loongson pts

yee pts

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell# awk 'BEGIN {FS="."}{print $1}' last.txt
按照新的分隔符“/”进行输出第一个自定义的域,方法二通过设置输入分隔符

root pts/6 172

yee pts/5 172

yee pts/1 172

root pts/6 172

yee pts/5 172

yee pts/1 172

loongson pts/6 172

yee pts/5 172

loongson pts/1 172

yee pts/5 172

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell#

Loong:/home/yee/shell# Es="/"

Loong:/home/yee/shell# awk -F $Es '{print $1}' last.txt 按照环境变量Es的值做为分隔符

root pts

yee pts

yee pts

root pts

yee pts

yee pts

loongson pts

yee pts

loongson pts

yee pts

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk -F '[/]''{print $1}' last.txt


root pts

yee pts

yee pts

root pts

yee pts

yee pts

loongson pts

yee pts

loongson pts

yee pts

wtmp begins Wed Oct 10 09:34:03 2012

Loong:/home/yee/shell# awk -F '[ :\t]''{print $1}' last.txt
按照正则表达式的值做为分隔符来显示指定输出

root

yee

yee

root

yee

yee

loongson

yee

loongson

yee

wtmp

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk '$1 ~/root/
{print $1}' last.txt


root

root

Loong:/home/yee/shell# awk '$1 ~/root/ {print $3}' last.txt 显示文件中第一个域匹配root的行并输出第三个域

172.16.7.94

172.16.7.94

Loong:/home/yee/shell# awk '{print
($1>$2 ? "high "$1: "low " $1)}' last.txt
(表达式1?表达式2:表达式3) 相当于:

if (表达式1)

表达式2

else

表达式3

high root 如果条件成立,则执行表达式2,否则执行表达式3

high yee

high yee

high root

high yee

high yee

low loongson

high yee

low loongson

high yee

low

high wtmp

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk '/172.16.7.94/{$3=1000;print}' last.txt 找到匹配行后为变量$3赋值并打印该行,也可print $3指定打印变量

root pts/6 1000 Wed Oct 31 10:09 still logged in

root pts/6 1000 Tue Oct 30 14:52 - 19:38 (04:45)

yee pts/1 1000 Tue Oct 30 08:34 - 19:38 (11:04)

loongson pts/6 1000 Mon Oct 29 15:12 - 19:23 (04:11)

loongson pts/1 1000 Mon Oct 29 08:53 - 19:24 (10:31)

yee pts/5 1000 Fri Oct 26 10:53 - 17:27 (06:33)

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk '/yee/ {count++;} END {print "yee was found "count" times"}' last.txt END表示在所有输入行处理完后进行处理

yee was found 6 times

Loong:/home/yee/shell#

Loong:/home/yee/shell# awk
'gsub(/\./,"dot");END {print $3}' last.txt
gsub函数用字符串替换. 再将结果输出

root pts/6 172dot16dot7dot94 Wed Oct 31 10:09 still logged in

yee pts/5 172dot16dot7dot97 Wed Oct 31 10:09 still logged in

yee pts/1 172dot16dot7dot95 Wed Oct 31 10:06 still logged in

root pts/6 172dot16dot7dot94 Tue Oct 30 14:52 - 19:38 (04:45)

yee pts/5 172dot16dot3dot89 Tue Oct 30 08:36 - 19:43 (11:06)

yee pts/1 172dot16dot7dot94 Tue Oct 30 08:34 - 19:38 (11:04)

loongson pts/6 172dot16dot7dot94 Mon Oct 29 15:12 - 19:23 (04:11)

yee pts/5 172dot16dot3dot89 Mon Oct 29 08:59 - 19:08 (10:08)

loongson pts/1 172dot16dot7dot94 Mon Oct 29 08:53 - 19:24 (10:31)

yee pts/5 172dot16dot7dot94 Fri Oct 26 10:53 - 17:27 (06:33)

Wed

Loong:/home/yee/shell#

在awk中如需调用环境变量一定不能出现在单引号内:

Flag=abcd

awk '{print '$Flag'}' 用法不对,引号不配对,结果依赖环境

awk '{print "$Flag"}' 结果为$Flag

awk "{print '$Flag'}" 结果为$Flag

awk "{print \"$Flag\"}" 结果为abcd

单双引号的差别是:shell对单引号中的内容不解释,直接传给awk,而对双引号中的内容解释后再传给awk.

从以上例子可以看出awk主要的用法就是对行、字符串的某些内容根据特定的判断字符或定义来进行修改,主要方法有

(1)利用自带的参数NF,FS等设条件;

(2)利用/字符/,设条件;

(3)利用正则表达式[字符]设条件

(4)使用==,if,else;while,自加;大于,小于;for循环等

最后进行指定输出。

更多用法参考:http://www.chinaunix.net/old_jh/24/691456.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: