一、vi命令
1、快速删除
清空当前编辑文件
在命令模式下,首先执行 gg
这里是跳至文件首行
再执行 dG
这样就清空了整个文件!
删除光标至行尾的内容 d$
2、多行编辑
在命令模式下,按Ctrl+v进入Visual-Block 模式,移动光标选择多行,如果删除按d即可删除多行,如果在行首插入,按Shift+i即可在多行插入,注释代码或取消注释非常有用。
二、文本处理
1、grep
利用grep提取进程号并杀死该进程
2、egrep
查看配置文件中配置项,去掉#注释及空格
egrep -v ‘^#|^$’ /etc/nova/nova.conf
3、sed
4、cut
cut用于字符串截取 cut -d “.” -f1-3
-f 点分截取,前3个段
# echo “192.168.200.1”|cut -d “.” -f1-3
192.168.200
-d \| 指定分割符|
# echo “hello|world”|cut -d \| -f2
world
-b、-c、-f分别表示字节、字符、字段
5、find
找到包含特定内容的文件
在某个路径下查找所有包含“hello abcserver”字符串的文件。
find /etc -name “*” | xargs grep “hello abcserver”
即
find .|xargs grep x
删除指定日期之前的文件
find /tmp -ctime +60 -type f -delete
#find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \;
假如在一个目录中保留最近30天的文件,30天前的文件自动删除
#find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \;
/tmp –设置查找的目录;
-mtime +30 –设置时间为30天前;
-type f –设置查找的类型为文件;
-name *.sh[ab] –设置文件名称中包含sha或者shb;
-exec rm -f –查找完毕后执行删除操作;
提示:将此命令写入crontab后即可自动完成查找并删除的工作
6、awk
7、tee
既能把输出保存到文件中,又能在终端上看到输出内容,使用tee命令,下例将hosts文件保存到new-file中
1 |
tee new-file < /etc/hosts |
三、正则表达式d
1、锚字符 ^ $
用来将模式锁定在数据流中的行首或行尾
行首匹配^
1 2 3 4 |
[root@compute01 ~]# echo "The book store" | sed -n '/^The/p' The book store [root@compute01 ~]# echo "The book store" | sed -n '/^book/p' [root@compute01 ~]# |
脱字符放到模式开头之外的其他位置,那么它就跟普通字符一样,不再是特殊字符
1 2 3 4 |
[root@compute01 ~]# echo "This ^ is a test" | sed -n '/s ^/p' This ^ is a test [root@compute01 ~]# echo "This is a test" | sed -n '/s ^/p' [root@compute01 ~]# |
行尾匹配
1 2 3 4 5 6 7 8 9 10 |
[root@controller01 ~]# cat test.txt this is a test of using both anchors I said this is a test this is a test I'm sure this is a test. [root@controller01 ~]# sed -n '/test$/p' test.txt I said this is a test this is a test [root@controller01 ~]# sed -n '/^this is a test$/p' test.txt this is a test |
组合匹配去除空白行
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@controller01 ~]# cat test.txt this is a test of using both anchors I said this is a test this is a test I'm sure this is a test. [root@controller01 ~]# sed '/^$/d' test.txt this is a test of using both anchors I said this is a test this is a test I'm sure this is a test. |
2、点号字符
点号用来匹配除换行符之外的任意单个字符。它必须匹配一个字符,如果在点号字符的位置没有字符,那么模式就不成立。
1 2 3 4 5 6 7 8 9 10 |
# cat test.txt This is a test of a line. The cat is sleeping. That is a very nice hat. This test is at line four. at ten o'clock we'll go home. [root@controller01 ~]# sed -n '/.at/p' test.txt The cat is sleeping. That is a very nice hat. This test is at line four. |
第五行因为.号位置没有字符,所有未能匹配。
实例一、修改配置文件
修改网卡的IP配置
1 2 3 4 5 |
[root@network01 ~]# sed -n '/IPADDR/p' ifcfg-eno16777736 IPADDR=192.168.2.14 [root@network01 ~]# sed -i -e 's/^IPADDR=.*/IPADDR=172.20.100.1/' ifcfg-eno16777736 [root@network01 ~]# sed -n '/IPADDR/p' ifcfg-eno16777736 IPADDR=172.20.100.1 |
四、Shell脚本相关
1、参数传递
$0为执行文件名,适用于./脚本、绝对路径脚本、bash 脚本等三种方式执行,. 脚本、source 脚本命令执行,输出“-bash”
$# 为传递给脚本的参数个数
$* 以“$1 $2 …. $n”输出所有参数
$@ 以“$1″ “$2” …. “$n”输出所有参数,在不使用””的情况下, 与$*是完全相同的,$@ 是分开的字串, $* 是一个字串,细致区别参考。
$$ 脚本运行的当前进程号
$! Shell最后运行的后台Process的PID
$? 显示脚本最后的退出状态,0表示无错误,其他值表示不同的错误标记,一些退出码(exit status,或exit code)的约定:
1 2 3 4 5 6 |
0表示成功(Zero - Success) 非0表示失败(Non-Zero - Failure) 2表示用法不当(Incorrect Usage) 127表示命令没有找到(Command Not Found) 126表示不是可执行的(Not an executable) >=128 信号产生 |
2、执行方式
3、重定向输出
参考:http://viplin.blog.51cto.com/241472/99568
30 19 * * * /usr/bin/**dcon.sh > /dev/null 2>&1
这里的/dev/null 2>&1 代表将标准输出和错误输出全部重定向到/dev/null中,也就是将产生的所有信息丢弃,
command > file 2>file 与command > file 2>&1区别:
- 首先~command > file 2>file 是将命令所产生的标准输出信息和错误的输出信息送到file 中.command > file 2>file 这样的写法,stdout和stderr都直接送到file中, file会被打开两次,这样stdout和stderr会互相覆盖,这样写相当使用了FD1和FD2两个同时去抢占file 的管道;
- 而command >file 2>&1 这条命令就将stdout直接送向file, stderr 继承了FD1管道后,再被送往file,此时,file 只被打开了一次,也只使用了一个管道FD1,它包括了stdout和stderr的内容;
- 从IO效率上,前一条命令的效率要比后面一条的命令效率要低,所以在编写shell脚本的时候,较多的时候我们会用command > file 2>&1 这样的写法。
五、文件压缩与解压
参考:http://www.cnblogs.com/eoiioe/archive/2008/09/20/1294681.html
1、.tar
2、.gz
3、.tar.gz 和 .tgz
4、.bz2
5、.tar.bz2
6、.bz
7、.tar.bz
8、.Z
9、.tar.Z
10、.zip
11、.rar
12、.lha
13、.rpm
14、.deb
15、sEx
code
more code
~~~~