Sed案例总结:
1.取出some.jpg的权限:
# stat some.jpg|sed -n 's#^.*(0\([0-7].*\)/-r.*$#\1#gp' 777 # stat process.py |sed -rn 's#^.*0([0-7]{3}).*$#\1#pg' 644 |
2.在config_ip.sh的第一行前加上#!/bin/bash
sed '1i\#!/bin/bash' config_ip.sh |
3.在config_ip.sh的第一行前加上空行
sed -i '1i\ ' config_ip.sh |
4.在hosts文件中加两个域名解析
sed '2a\192.168.1.20 jinfen\n192.168.1.21 xiaoming' /etc/hosts 127.0.0.1 localhost localhost.localdomain VM_137_230_centos www-jfedu-net ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.20 jinfen 192.168.1.21 xiaoming |
5.取出config_ip.sh的第二行和第六行的内容
sed -n '2p;6p' config_ip.sh #!/bin/bash while true sed -n -e '2p' -e '6p' config_ip.sh #!/bin/bash while true |
6..取出config_ip.sh的第二行到第六行的内容
sed -n '2,6p' config_ip.sh |
7.优化ssh配置文件(优化的参数见下):
1. Port 52113 2. PermitRootLogin no 3. PermitEmptyPasswords no 4. UseDNS no 5. GSSAPIAuthentication no |
sed -n 's/#Port.*$/Port 52113/pg' /etc/ssh/sshd_config Port 52113 |
sed -n 's/#PermitRootLogin.*$/PermitRootLogin no/gp' /etc/ssh/sshd_config PermitRootLogin no |
8.提取出某一个字符:
echo I am to like you|sed 's#^.*o \([a-z].*\) y.*$#\1#g' like |
echo I am to like you|sed -r 's#^.*o ([a-z]+) y.*$#\1#g' like //-r 扩展正则 |
9.系统开机启动项优化:
chkconfig --list|grep '3:on'|grep -vE 'sshd|crond|network|reysstat|sysstat'|awk '{print $1}'|sed -r 's#^(.*)#chkconfig \1 off#g'|bash |
10.特殊符号&代表被替换的内容:
echo -e "hello\nword"|sed 's#hello#-----&------#g'-----hello------word |
11.取出含有root的行:
sed -n '/root/p' /etc/passwdgrep 'root' /etc/passwd |
12.取出含有第20行到含有mysql的行
sed -n '20,/mysql/p' /etc/passwd |
13.将17/Apr/2015:09:29:24 +0800改为2015-04-17 09:29:24+0800的格式.
echo "17/Apr/2015:09:29:24 +0800"|sed -nr "s#(..)/(...)/(....):(.*) (.*)#\3-\2-\1 \4\5#;s#Apr#04#p"2015-04-17 09:29:24+0800 |
14.一键完成ssh的参数配置:
sed -i '13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no' sshd_config |
15.取出IP地址:
ifconfig eth0|sed -nr 's#.*addr:(.*) Bca.*$#\1#p'10.104.137.230ifconfig eth0|sed -n 's#.*ddr:##;s# Bca.*##p'10.104.137.230 |
16.替换命令的使用:将第一行换成this is root
# sed '1c\this is root' /etc/passwdthis is root |
17.删除命令的使用:删除以xiaodong开头的行
sed '/^xiaodong.*$/d' /etc/passwdsed '4,$d' /etc/passwd |
18.打印出奇数行:
seq 10|sed 'n;d' |
19.打印出偶数行:
seq 10|sed -n 'n;p' |
20.在每一行后追加一个空行
# seq 10|sed 'G' |
21.添加行前编号:
sed = /etc/fstab |sed 'N;s/\n/\./'1./dev/vda1 / ext3 noatime,acl,user_xattr 1 |
22.在song前加一个行前编号
sed -e '/song/s/^/&1./g' passwd1.song1:x:20533:20533::/home/song1:/bin/bash |
23.使用变量:一对单引号包着一对双引号
name=jiajiesed -n '/'"$name"'/p' 1.ah |
Grep案例总结
1.打印出出现root的行;
grep 'root' /etc/passwd |
2.打印出出现root的行并加上行号
grep -n 'root' /etc/passwd |
3.打印出空行和以#开头的行的之外的部分
egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_server.conf |
Centos7.不支持egrepgrep -E -v '^#|^ *#|^$' conf/nginx.conf |
4.打印出出现jfedu1和jfedu2的行。[12]代表配置里面的任一个字节。
grep -n 'jfedu[12]' /etc/passwd32:jfedu1:x:510:510::/home/jfedu1:/bin/bash33:jfedu2:x:511:511::/home/jfedu2:/bin/bash |
5.反向选择。 [^1]代表匹配除了jfedu1以外的含有jfedu的内容
grep -n 'jfedu[^1]' /etc/passwd31:jfedu:x:509:509::/home/jfedu:/bin/bash33:jfedu2:x:511:511::/home/jfedu2:/bin/bash34:jfedu999:x:512:512::/tmp//jfedu999:/bin/bash35:jfedu666:x:513:513::/home/jfedu666:/bin/bash49:jfedu001:x:20532:20532::/home/jfedu001:/bin/bash |
6.匹配出含有数字的行。
grep '[0-9]' which.sh |
7.匹配出以#号开头的行
grep '^#' which.sh |
8.找出非空白的行
grep -v '^$' which.sh |
9.找出含有j…u的行。. 号表示一个字节
grep 'j...u' /etc/passwd |
10.匹配出IP地址。[0-9]{1,3}代表匹配前面的数字1~3次
ifconfig eth0|egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'+ 代表匹配前面出现的字符一次或者多次。-o 只打印匹配的字符ifconfig eth0|egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' |
11.输出含有root的行数
grep -c 'root' /etc/passwd |
12.取出访问频率最高的前10个IP地址
egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' access_log|sort|uniq -c|sort -nr|head -10 |
13.查看mysql进程
ps -ef|grep mysql|grep -v grep |
14.查看mysql端口是否起来
ss -tnl|grep 3306LISTEN 0 50 *:3306 *:* |
15.使用 –l 参数,列出包含root参数的文件名(查询多个文件时)
grep -l root /etc/passwd /etc/shadow /etc/fstab /etc/issue/etc/passwd/etc/shadow |
16.输出匹配行的前四行,后四行,前后四行
grep -B 4 xiaoming /etc/passwd //前四行 beforegrep -A 4 xiaoming /etc/passwd //后四行 aftergrep -C 4 xiaoming /etc/passwd //前后四行 |
17.将搜索的关键字标红
grep --color=auto 'root' /etc/passwd |
18.查找当前目录下以.txt结尾的文件。
ll |grep '.txt'-rw-r--r-- 1 root root 0 Aug 24 11:29 1.txt-rw-r--r-- 1 root root 139018 Aug 23 16:29 all_php.txt-rw-r--r-- 1 root root 97 Aug 23 14:58 jfedu.txt |
19.匹配含有空行的
grep '[[:space:]]' jfedu.txt |
20.匹配单行含有标点符号的内容
grep '[[:punct:]]' jfedu.txt |
Awk案例总结:
1.取出IP地址:
ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $4}' |
2.显示最近登录的5个用户:
last -n 5|awk '{print $1}' |
3.显示/etc/passwd的账号名
awk -F: '{print $1}' /etc/passwd |
4.在第一行添加name,shell,并在最后一行添加ending,/bin/bash
cat /etc/passwd|awk -F: 'BEGIN{print "name,shell"}{print $1","$7}END{print "ending,/bin/bash"}' |
5.搜索关键字所在的行。(由此可以看出在一定条件下三剑客之间的功能是一样的)
awk '/root/' /etc/passwdgrep root /etc/passwdsed -n '/root/p' /etc/passwd |
6.搜索root所在的行,并显示对应的shell
awk -F: '/root/{print $1"-"$7}' /etc/passwd |
7.定制/etc/passwd显示的内容
awk -F: '{print "filename:"FILENAME",linenumber:"NR",column:"NF",linecontent:"$0}' /etc/passwdawk -F: '{printf("filename:%10s,linenumber:%s,columns:%s,linecontent:%s\n",FILENAME,NR,NF,$0)}' /etc/passwd |
8.查看用户的总数。
awk 'BEGIN{count=0}{count++;print $0;}END{print "user count is",count}' /etc/passwd |
9.统计某个文件夹下的文件占用的字节数
ls -l|awk 'BEGIN{size=0}{size=size+$5}END{print "size is " size/1024/1024,"M"}'size is 44.5918 M |
10. 统计某个文件夹下的文件占用的字节数过滤出4096大小的文件(一般是文件夹)
ls -l|awk 'BEGIN{size=0;print "[START]size is",size}{if($5!=4096){size=size+$5;}}END{print"[END]size is",size/1024/1024,"M"}'[START]size is 0[END]size is 44.5606 M |
11.使用for循环遍历数组
awk -F: 'BEGIN{count=0;}{name[count]=$1;count++;}END{for(i=0;i<NR;i++)print i,name[i]}' /etc/passwd |
12.精确匹配出root用户的行
awk -F: '$1=="root"' /etc/passwdroot:x:0:0:root:/root:/bin/bash |
13.列出有多少列
echo "I am a bird"|awk '{print NF}'4 |
14.输出最后一个字段的内容
echo "I am a bird"|awk '{print $NF}'bird |
15.指定输出分隔符
echo "I am a bird"|awk 'BEGIN{OFS="#"}{print $1,$2,$3,$4}'I#am#a#bird |
16.打印出含有tcp 的行
awk '/tcp/{print $0}' test.txt |
17.逻辑||
awk '/blp5/||/3gpp/{print $0}' test.txt |
18.逻辑&&
awk '/blp5/&&/tcp/{print $0}' test.txt |
19.打印出3gpp到blp5的行
awk '/3gpp/,/blp5/{print $0}' test.txt |
20.统计出空白行的总个数
awk '/^$/{x+=1}END{print x}' /etc/ssh/ssh_config4 |
21.判断boot分区可用容量小于20M时报警,否则就显示OK
df|grep 'boot'|awk '{if($4<20000)print"alart";else print "ok"}' |
22.判断
seq 5|awk '{if($0==3)print $0;else print "no"}' |
23.显示IP
ifconfig eth0|awk '/Bcast/'|awk -F'[ :]+' '{print $4}' |
23.统计访问IP
awk '/2017:16:52/,/2017:15:21/' access_log|awk '{print $1}'|sort|uniq -c|sort -nr|head -10 |
awk -F"=" '{print "rpm -e --nodeps "$2}' test.txt
Find命令总结:
语法格式:
find path -option [ -print ] [ -exec -ok command ] { } \; |
1.列出当前目录及其子目录下的所有文件及其文件夹:
find . [-print] |
2.根据文件名来查找:
# find . -name "*.txt" ./jfedu.txt ./.jenkins/userContent/readme.txt ./.jenkins/jobs/www.jfedu.net/svnexternals.txt |
3.忽略大小写:
# find . -iname "*.txt" ./jfedu.txt ./.jenkins/userContent/readme.txt ./.jenkins/jobs/www.jfedu.net/svnexternals.txt ./index.TXT |
4.匹配多个条件中的 -o(or) -a(and)
# find . -name "*.txt" -o -name "*.pdf" ./n.txt ./new.txt ./text.pdf # find . -name "*.txt" -a -name "n.*" ./n.txt |
5.指定最大深度查找目录 -mindepth 遍历最小深度
# find . -maxdepth 2 -type d . ./.jenkins ./.jenkins/userContent ./.jenkins/fingerprints ./.jenkins/plugins ./.jenkins/cache ./.jenkins/updates |
6.根据文件类型搜索:
# find . -type f ./n.txt ./which.sh f:普通文件 d:目录 c:块设备 l:符号链接 b:套接字 |
7.根据文件时间进行搜索:
# find . -type f -atime -7 打印7天内被访问过的文件 ./n.txt ./which.sh ./echo_hello.sh ./new.txt -atime 访问时间 -mtime 文件的最后一次修改时间 -ctime 文件的变化时间(如:权限) 以下是基于分钟的: -amin 访问时间 -mmin 修改时间 -ctime 变化时间 + 大于 –小于 |
8.指定比较的时间戳的参考文件,然后找出比参考问间新的文件:
# find . -type f -newer which.sh ./n.txt ./new.txt ./text.pdf ./some.jpg |
9.基于文件的大小进行文件搜索:
# find . -type f -size +1M //大于1M的文件 ./.jenkins/plugins/maven-plugin.jpi ./.jenkins/plugins/subversion.jpi ./.jenkins/plugins/subversion/WEB-INF/lib/svnkit-1.7.10-jenkins-1.jar b 块设备 c 字节 w 字 k 千字节 M兆字节 G 吉字节 |
10.删除匹配的文件:-delete
# find . -type f -name "*.swp" -delete |
11.匹配文件权限:-perm
# find . -type f -perm 644 |
12.根据文件的所有权进行搜索:-user
# find / etc -type f -user mysql |
13.结合find执行命令或者动作:
找到/tmp目录下所有权不是root的文件,并修改成root权限 # find /tmp/ -type f ! -user root -exec chown root {} \; 找到所有php文件,读取放到all_php.txt文件中 # find . -type f -name "*.php" -exec cat {} \;>all_php.txt 将10天前的文件复制到old中 # find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \; |