BASH特性
bash
# history命令
-c 清楚历史记录
-r 恢复历史记录
# 调用历史记录命令
# !加历史id,快速执行历史命令
!362
# !! 执行上次的命令,以及上下左右寻找
!!
shell变量
bash
root@lisheng-VMware20-1:~# echo $music
大碗宽面
root@lisheng-VMware20-1:~# echo ${music}
大碗宽面
单引号变量不识别特殊语法
双引号变量能识别特殊语法
bash
lisheng@lisheng-VMware20-1:~$ name='${me}'
lisheng@lisheng-VMware20-1:~$ echo $name
${name}
lisheng@lisheng-VMware20-1:~$ name2="${name}"
lisheng@lisheng-VMware20-1:~$ echo $name2
me
特殊变量学习
参数传递
bash
位置参数的获取
$0 获取文件名
$1 获取第一个参数
$2 ....
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ cat >touch_a.txt<< EOF
> 1
> 2
> 3
> EOF
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ cat touch_a.txt
1
2
3
# 以前的数据会被清空
grep
bash
# -n现实行号
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep -n hello touch_a.txt
1:hello
# -v显示不包含lisi的
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep -v "lisi" touch_a.txt
hello
world
wangwu text
# -i不区分大小写
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep Lisi touch_a.txt
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep -i Lisi touch_a.txt
lisi text
# -E支持正则表达式
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep -E "lisi||hello" touch_a.txt
hello
world
lisi text
wangwu text
# -c显示匹配数量
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ grep -c "lisi" touch_a.txt
1
sed:流编辑器
测试文件内容
bash
I am zhangsan
I like badminton ball, billiard ball and chinese
chess my site is http://www.zhangsan.com
my telphone is 123456
输出第2-3行的内容
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ sed -n '2,3p' touch_a.txt
I like badminton ball, billiard ball and chinese chess
my site is http://www.zhangsan.com
过滤出含有zhangsan字符串的行
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ sed -n '/zhangsan/p' touch_a.txt
I am zhangsan
my site is http://www.zhangsan.com
删除含有zhangsan字符串的行,默认不会修改文件
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ sed '/zhangsan/d' touch_a.txt
I like badminton ball, billiard ball and chinese chess
my telphone is 123456
删除指定的行
bash
sed -i '3d' touch_a.txt
sed -i '5,8d' touch_a.txt
将文件中的zhangsan字符串全部替换为lisi,默认不会修改文件
bash
sed 's#zhangsan#lisi#g' touch_a.txt
将文件中的zhangsan字符串全部替换为lisi,同时将telephone号码123456改为654321
bash
sed -e 's#zhangsan#lisi#g' -e 's#123456#654321#g' touch_a.txt
在touch_a.txt文件的第二行后追加文本
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ sed '2a hello' touch_a.txt
I am zhangsan
I like badminton ball, billiard ball and chinese chess
hello
my site is http://www.zhangsan.com
my telphone is 123456
在touch_a.txt文件的第二行插入文本
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ sed '2i test' touch_a.txt
I am zhangsan
test
I like badminton ball, billiard ball and chinese chess
my site is http://www.zhangsan.com
my telphone is 123456
awk
准备测试文件
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ cat awk_text.txt
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
1p:x:4:7:1p:/var/spool/1pd:/sbin/nologin
取文件第二行到第三行的内容
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk 'NR>1&&NR<5' awk_text.txt
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字符串的行
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk '/root/' awk_text.txt
root:x:0:0:root:/root:/bin/bash
过滤出以非r开头的行
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk '/[^r]/' awk_text.txt
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
1p:x:4:7:1p:/var/spool/1pd:/sbin/nologin
取文件的第一列、第三列和最后一列的内容,并打印引号
bash
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk -F ":" '{print NR,$1,$3,$NF}' awk_text.txt
1 root 0 /bin/bash
2 bin 1 /sbin/nologin
3 daemon 2 /sbin/nologin
4 adm 3 /sbin/nologin
5 1p 4 /sbin/nologin
处理以下文件内容,将域名取出并根据域名进行计数排序处理
bash
http://www.etiantian.org/index.html
http://www.etiantian.org/1.html
http://post.etiantian.org/index.html
http://mp3.etiantian.org/index.html
http://www.etiantian.org/3.html
http://post.etiantian.org/2.html
bash
# 取出每行中的域名:
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk -F '/' '{print $3}' awk_text.txt
www.etiantian.org
www.etiantian.org
post.etiantian.org
mp3.etiantian.org
www.etiantian.org
post.etiantian.org
# 排序
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk -F '/' '{print $3}' awk_text.txt|sort
mp3.etiantian.org
post.etiantian.org
post.etiantian.org
www.etiantian.org
www.etiantian.org
www.etiantian.org
# 去重计数
lisheng@lisheng-VMware20-1:~/Desktop/mkdir_a$ awk -F '/' '{print $3}' awk_text.txt|sort|uniq -c
1 mp3.etiantian.org
2 post.etiantian.org
3 www.etiantian.org