Chinaunix首页 | 论坛 | 博客
  • 博客访问: 423272
  • 博文数量: 158
  • 博客积分: 1855
  • 博客等级: 上尉
  • 技术积分: 1888
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-18 14:19
文章分类

全部博文(158)

文章存档

2013年(4)

2012年(16)

2011年(10)

2010年(40)

2009年(61)

2008年(33)

分类:

2008-09-20 17:22:31

1、查找含有IP的文件:#grep "[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1-3\}" *(所有文件,*.txt以.txt为后缀的所有文件)
#grep "[0-9]\{3\}.[0-9]\{3\}.[0-9]\{3\}.[0-9]\{3\}" * 只能查找***.***.***.***这种格式的IP,而如192.168.0.2这样的就不匹配了
2、查找当前目录下哪些是目录文件
   #ls -l|grep "^d" (ls -l的结果中以d开头的为目录)
   #ls -l|grep "^[^d]" (查看ls -l的结果中不是以d开头的文件) 同#ls -l|grep ^d -v(-v取反)
grep ^root /etc/passwd (查找/etc/passwd中以root开头的行)
grep -v ^root /etc/passwd (查找/etc/passwd中不以root开头的行)
grep bash$ /etc/passwd (查找/etc/passwd中以bash结尾的行)
grep ^$ /etc/passwd (查找/etc/passwd中的空行)
3、查找一个文件中含有大写字母的行
   #grep "[[:upper:]]" /etc/profile
   或#grep -n "[[:upper:]]" /etc/profile (-n显示匹配行及行号)
    #grep -n "[[:upper:]]" /etc/profile |wc -l (含有大写字母的行数)
 grep命令类名:
 [[:upper:]] 相当于[A-Z]
 [[:lower:]] 相当于[a-z]
 [[:digit::]相当于[0-9]
 [[:alpha:]] 相当于[a-zA-Z]
 [[:space:]] 空格或tab键
 [[:alnum:]]相当于[0-9a-zA-Z]
 
 
[root@server0 b]# cat txt
/var/log/Xorg.setup.log
/var/log/gdm/:0.log
/var/log/anaconda.log
/var/log/scrollkeeper.log
 
[root@server0 b]# awk -F '/' '{print NF,NR,ENVIRON["USER"],$4}' txt
4 1 root Xorg.setup.log
5 2 root gdm
4 3 root anaconda.log
4 4 root scrollkeeper.log
[root@server0 b]#
NF浏览记录的域个数,NR已读的记录数,gsub(s,t)用t来替换s
[root@server0 b]# awk -F '/' '{if(gsub("/","#")) print $0}' txt
#var#log#Xorg.setup.log
#var#log#gdm#:0.log
#var#log#anaconda.log
#var#log#scrollkeeper.log
[root@server0 b]#
[root@server0 b]# awk 'BEGIN {split("aa#bb#cc",array,"#");print array[1],"\t",array[2],"\t",array[3]}'
aa       bb      cc  注:awk中的数组是从1开始的,不同于C语言中从0开始
[root@server0 b]#
 
#!/bin/awk -f
#awk.sh
BEGIN{
 FS="#" # -F
 score["0-60"]=0
 score["60-70"]=0
 score["70-80"]=0
 score["80-90"]=0
 score["90-100"]=0
 student["junior"]=0
 student["senior"]=0
}
 {
   {if($1<60)
    score["0-60"]++
   }
   {if($1<70&&$1>=60)
    score["60-70"]++
   }
   {if($1<80&&$1>=70)
    score["70-80"]++
   }
   {if($1<90&&$1>=80)
     score["80-90"]++
   }
   {if($1<=100&&$1>=90)
     score["90-100"]++
   }
 }
 {
   for(juniorsenior in student)
    {if($2==juniorsenior)
     student[juniorsenior]++
    }
 }
END{
     { for(number in score)print "the score",number,"has",score[number],"students"}
     {for(juniorsenior in student)print "the class has",student[juniorsenior],"students"}
}  
注:符号"{"要直接跟在BEGIN,END后面
[root@server0 b]# cat grade
35#senior
45#junior
65#junior
69#senior
69#junior
45#senior
72#junior
78#senior
80#junior
85#junior
90#senior
91#junior
96#junior
[root@server0 b]#
 
[root@server0 b]# ./awk.sh grade
the score 0-60 has 3 students
the score 70-80 has 2 students
the score 90-100 has 3 students
the score 60-70 has 3 students
the score 80-90 has 2 students
the class has 5 students
the class has 8 students
[root@server0 b]#
 
 
#!/bin/bash
#datelog
current_date=`date "+%Y%m%d"`
echo $current_date
todaylog="log/${current_date}.log" #当前目录下必须有log目录,否则会提示log目录或文件不存在
echo $todaylog
#if [ ! -f $todaylog ]
#then
#   touch $todaylog
#fi   #这四行可以注释掉     
log_time_format=`date "+%Y-%m-%d %T"`
echo "$log_time_format command begin">>$todaylog
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
sleep 4 #停留4秒
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
log_time_format=`date "+%Y-%m-%d %T"`  # %T小时:分钟:秒
echo "$log_time_format command end">>$todaylog

[root@server0 b]#
 
在脚本中捕捉信号:
[root@server0 ~]# cat trap1.sh
#!/bin/bash
#trap1.sh
trap "exitprocess" 2 注:信号2的含义:来自键盘的中断信号,通常是
loop=0
function exitprocess()
{
 echo "you just hit ,at number $loop"
 echo "I will now exit"
 exit 1
}
while :
do
  loop=$[$loop+1]
  echo $loop
  sleep 1
done
[root@server0 ~]#

 
阅读(1379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~