awk
[root@localhost ~]# awk '{print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost ~]# awk '{print ""}' /etc/passwd
[root@localhost ~]# awk '{print "hihi"}' /etc/passwd
hihi
hihi
hihi
hihi
[root@localhost ~]# awk -F":" '{print $1 ":" $2}' /etc/passwd
root:x
bin:x
daemon:x
adm:x
awk 编程
NF:输出的字段个数
1)awk '{print NF}'
2)awk '{print $NF}' 读取输出行的第 个字段
3)awk 'BEGIN{}'
awk 'BEGIN {FS="[:, ]"} print $0' /etc/passwd
awk 'BEGIN {print toupper("hello"); print tolower("WORLD")}'
4)awk '/pattern/{action}' file
zdd.txt
t Price/lbs
Banana 0.89
Paech 0.79
Kiwi 1.50
Pineapple 1.29
Apple 0.99
[root@localhost ~]# awk '/[1-9]\.[0-9][0-9]$/ {print $0,"*"}' zdd.txt
Kiwi 1.50 *
Pineapple 1.29 *
5)awk -F:'{}'
[root@localhost ~]# echo $PATH |awk -F: '{
> for(i=1;i<=NF;i++){
> printf "%s\n",$i;
> }
> }'
/usr/local/software/mysql/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/usr/local/software/erlang/bin
/root/bin
[root@localhost ~]# echo $PATH
/usr/local/software/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin
6)awk '条件{}' file
awk '$2>=1.0{printf "%s\t%s\n",$0,"Expensive";}' zdd.txt
t Price/lbs Expensive
Kiwi 1.50 Expensive
Pineapple 1.29 Expensive
7)
awk '{printf "%-15s %s\n",$1,$2;}' zdd.txt
t Price/lbs
Banana 0.89
Paech 0.79
Kiwi 1.50
Pineapple 1.29
Apple 0.99
8)awk '{OFS="";print }'
[root@localhost ~]# echo "cat wu sensation" | awk '{OFS="..";print $0}'
cat wu sensation
[root@localhost ~]# echo "cat wu sensation" | awk '{OFS="..";$1=$1;print $0}'
cat..wu..sensation
[root@localhost ~]# echo "cat wu sensation" | awk '{OFS="..";print $1,$2,$3}'
cat..wu..sensation
[root@localhost ~]# echo "cat wu sensation" | awk '{OFS="..";print $1,$2,$3,$4}'
cat..wu..sensation..
9)getline
1>command |getline :将command 的结果赋值给$0
command |getline var:将command的结果赋值给var
2>root@localhost ~]# cat 2.txt|awk 'BEGIN{getline;print $0}'
100 200 300
[root@localhost ~]# cat 2.txt|awk 'BEGIN{getline d;print $0}'
[root@localhost ~]# cat 2.txt|awk 'BEGIN{getline d;print d;}'
100 200 300
3>[root@localhost ~]# awk 'BEGIN{while(getline<"/etc/passwd">0);print $1;}'
xiaodong2:x:549:549::/usr/home/xiaodong2:/bin/bash
[root@localhost ~]# awk 'BEGIN{while("who -u"|getline) print $1}'
root
root
root
10)
ARGC 命令行变元个数
ARGV 命令行变元数组
FILENAME 当前输入文件名
FNR 当前文件中的记录号
FS 输入域分隔符,默认为一个空格
RS 输入记录分隔符
NF 当前记录里域个数
NR 到目前为止记录数
OFS 输出域分隔符
ORS 输出记录分隔符
11)打开外部文件
awk 'BEGIN{while(cat "/etc/passwd"|getline){print $0;};close("/etc/passwd")}'
awk 'BEGIN{while(getline < "/etc/passwd"){print $0;};close("/etc/passwd")}'
12)
Flag=abcd
awk '{print '$Flag'}' 结果为abcd
awk '{print "$Flag"}' 结果为$Flag
sed
1)ns :替换
压缩空白:sed 's/^$//g'
2)nd:删除
1>sed '1d' file 删除第一行
sed '2,$d' file 删除第二行到最后一行
2>2.txt
100 200 300
rr aa bb
ciejo2
[root@localhost ~]# sed '/[0-9]/d' 2.txt
rr aa bb
3) -n :显示
sed -n '1p' file 显示第一行
sed -n '2,$p' file 显示第二行到最后一行
sed -n '/100/p' 1.txt 显示包含100的行
100 9 19 20
100 89 70 80
[root@localhost ~]# sed -n '/100/p' 1.txt
100 9 19 20
100 89 70 80
[root@localhost ~]# sed -n 's/100//p' 1.txt
9 19 20
89 70 80
4)sed -y:对应着替换
sed -y '/[a-z]/[A-Z]/g'
5)sed -e:跟一个表达式
sed -e expression,-e expression = sed -e expression,expression
6)
'i':前插
‘a’:后插
sed 'na ajjj ' file :第n行增加ajjjj
[root@localhost ~]# sed '1a abc efg' 1.txt
100 9 19 20
abc efg
100 89 70 80
300 546 12 00
7)sed 'nc ssiejie' file :第n行内容 替换为ssiejie
[root@localhost ~]# sed '1c ssie' 1.txt
ssie
100 89 70 80
300 546 12 00
8)匹配子串
sed '/\([0-9]\{\+3\}\)/abc\1/'
[root@localhost ~]# echo seven EGIGHT |sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
EGIGHT seven
[root@localhost ~]# echo 8349aFde|sed 's/[0-9]+\([a-zA-Z]\+\)/\1/'
8349aFde
[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)+\([a-zA-Z]\+\)/\1/'
8349aFde
[root@localhost ~]# echo 8349aFde|sed 's/\([0-9]\+\)\([a-zA-Z]\+\)/\1/'
8349
9)[root@localhost ~]# echo /***eeioejiowjioejo****/|sed ':/\*.*\*/::g'
/***eeioejiowjioejo****/
[root@localhost ~]# echo /***eeioejiowjioejo****/|sed 's:/\*.*\*/::g'