shell--1.小工具

1.小工具学习

1.grep 行过滤工具

grep [选项] ‘关键字’ 文件名

OPTIONS:
    -i: 不区分大小写
    -v: 查找不包含指定内容的行,反向选择
    -w: 按单词搜索
    -o: 打印匹配关键字
    -c: 统计匹配到的行数
    -n: 显示行号
    -r: 逐层遍历目录查找
    -A: 显示匹配行及后面多少行	
    -B: 显示匹配行及前面多少行
    -C: 显示匹配行前后多少行
    -l:只列出匹配的文件名
    -L:列出不匹配的文件名
    -e: 使用正则匹配
    -E:使用扩展正则匹配
    ^key:以关键字开头
    key$:以关键字结尾
    ^$:匹配空行
    --color=auto :可以将找到的关键词部分加上颜色的显示

例如

[root@node2 shell_study]# cp /etc/passwd .
[root@node2 shell_study]# ll
总用量 4
-rw-r--r--. 1 root root 798 1018 12:44 passwd
[root@node2 shell_study]# 

[root@node2 shell_study]# grep 'root' passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

如果您电脑grep执行完以后,匹配的文字没有带颜色,可以将起别名grep ,让grep命令等同于grep --color=auto,这样执行grep过滤行信息的时候,就可以带颜色了

如下是起别名的方式

临时设置:
# alias grep='grep --color=auto'			//只针对当前终端和当前用户生效

永久设置:
1)全局(针对所有用户生效)
vim /etc/bashrc
alias grep='grep --color=auto'
source /etc/bashrc

2)局部(针对具体的某个用户)
vim ~/.bashrc
alias grep='grep --color=auto'
source ~/.bashrc
# grep -i root passwd						忽略大小写匹配包含root的行
# grep -w ftp passwd 						精确匹配ftp单词
# grep -w hello passwd 						精确匹配hello单词;自己添加包含hello的行到文件
# grep -wo ftp passwd 						打印匹配到的关键字ftp
# grep -n root passwd 						打印匹配到root关键字的行好
# grep -ni root passwd 						忽略大小写匹配统计包含关键字root的行
# grep -nic root passwd						忽略大小写匹配统计包含关键字root的行数
# grep -i ^root passwd 						忽略大小写匹配以root开头的行
# grep bash$ passwd 							匹配以bash结尾的行
# grep -n ^$ passwd 							匹配空行并打印行号
# grep ^# /etc/vsftpd/vsftpd.conf		匹配以#号开头的行
# grep -v ^# /etc/vsftpd/vsftpd.conf	匹配不以#号开头的行
# grep -A 5 mail passwd 				 	匹配包含mail关键字及其后5行
# grep -B 5 mail passwd 				 	匹配包含mail关键字及其前5行
# grep -C 5 mail passwd 					匹配包含mail关键字及其前后5行

在passwd 输入

hello world
helloworld

[root@node2 shell_study]# grep hello passwd 
hello world
helloworld
[root@node2 shell_study]# grep -w hello passwd -w 精准匹配hello,helloworld匹配不成功
hello world
[root@node2 shell_study]# grep -o hello passwd   -o 打印关键字本身
hello
hello
[root@node2 shell_study]# grep -wo hello passwd 
hello

实际应用中查看日志

grep -C 10 -i error 日志文件

2. cut 列截取工具

cut 选项 文件名

-c:	以字符为单位进行分割,截取
-d:	自定义分隔符,默认为制表符\t
-f:	与-d一起使用,指定截取哪个区域
# cut -d: -f1 1.txt 			以:冒号分割,截取第1列内容
# cut -d: -f1,6,7 1.txt 	以:冒号分割,截取第1,6,7列内容
# cut -c4 1.txt 				截取文件中每行第4个字符
# cut -c1-4 1.txt 			截取文件中每行的1-4个字符
# cut -c4-10 1.txt 			截取文件中每行的4-10个字符
# cut -c5- 1.txt 				从第5个字符开始截取后面所有字符

显示头几行,以:分隔,显示第一个元素

[root@node2 shell_study]# head passwd | cut -d: -f1 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@node2 shell_study]# head 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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

练习
查看系统的运行级别

[root@node2 shell_study]# runlevel   ---系统的运行级别
N 3
[root@node2 shell_study]# runlevel | cut -c1
N
[root@node2 shell_study]# runlevel | cut -c3
3
[root@node2 shell_study]# runlevel |cut -d ' ' -f2
3
runlevel |cut -c3
runlevel | cut -d ' ' -f2
grep -v '^#' /etc/inittab | cut -d: -f2
grep '^id' /etc/inittab |cut -d: -f2
grep "initdefault:$" /etc/inittab | cut -c4
grep -v ^# /etc/inittab |cut -c4
grep 'id:' /etc/inittab |cut -d: -f2
cut -d':' -f2 /etc/inittab |grep -v ^#
cut -c4 /etc/inittab |tail -1
cut -d: -f2 /etc/inittab |tail -1

3. sort --排序

sort工具用于排序;它将文件的每一行作为一个单位,从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出

-u :去除重复行
-r :降序排列,默认是升序
-o : 将排序结果输出到文件中,类似重定向符号>
-n :以数字排序,默认是按字符排序
-t :分隔符
-k :第N列
-b :忽略前导空格。
-R :随机排序,每次运行的结果均不同
# sort -n -t: -k3 1.txt 			按照用户的uid进行升序排列
# sort -nr -t: -k3 1.txt 			按照用户的uid进行降序排列
# sort -n 2.txt 						按照数字排序
# sort -nu 2.txt 						按照数字排序并且去重
# sort -nr 2.txt 
# sort -nru 2.txt 
# sort -nru 2.txt 
# sort -n 2.txt -o 3.txt 			按照数字排序并将结果重定向到文件
# sort -R 2.txt 
# sort -u 2.txt 

将passwd文件的前10行写到1.txt中

[root@node2 shell_study]# head passwd > 1.txt
[root@node2 shell_study]# cat -n 1.txt 
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
[root@node2 shell_study]# 

以用户id ,按照数字进行升序排列

[root@node2 shell_study]# sort -n -t: -k3 1.txt | cat -n
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
     7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8  halt:x:7:0:halt:/sbin:/sbin/halt
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10  operator:x:11:0:operator:/root:/sbin/nologin
[root@node2 shell_study]# 

4. uniq工具–去重重复行

常见选项:
-i: 忽略大小写
-c: 统计重复行次数
-d:只显示重复行

举例说明:
# uniq 2.txt 
# uniq -d 2.txt 
# uniq -dc 2.txt 

vi 3.txt

aaa
111
aaa
444
555
555
555
111
aaa
aaa
[root@node2 shell_study]# uniq 3.txt
aaa
111
aaa
444
555
111
aaa
[root@node2 shell_study]# uniq -c 3.txt
      1 aaa
      1 111
      1 aaa
      1 444
      3 555
      1 111
      2 aaa
[root@node2 shell_study]# uniq -cd 3.txt
      3 555
      2 aaa

5. tee工具–双向输出

tee工具是从标准输入读取并写入到标准输出和文件,即:双向覆盖重定向(屏幕输出|文本输入)

选项:
-a 双向追加重定向

# echo hello world
# echo hello world|tee file1
# cat file1 
# echo 999|tee -a file1
# cat file1 

tee 的作用是一方面可以界面输出内容,同时将内容输入到文件中

\命令> 和>>也可以输入到文件中,>覆盖写入, >>追加写入

练习

vi vsftpd.conf

如下是内容
# this is a demo

# this conent is temp file

local_ip=YES
listent=YES
is_bind=NO

write_able=YES
param=NO

listenter=YES

要求去掉空行,去掉#开头的行,将内容输出到文件5.txt中,并在控制台中打印出来

[root@node2 shell_study]# grep -v '^#' vsftpd.conf |grep -v '^$' | tee 5.txt
local_ip=YES
listent=YES
is_bind=NO
write_able=YES
param=NO
listenter=YES
[root@node2 shell_study]# cat -n 5.txt
     1  local_ip=YES
     2  listent=YES
     3  is_bind=NO
     4  write_able=YES
     5  param=NO
     6  listenter=YES
[root@node2 shell_study]# 

6. diff工具—比较文件不同

diff工具用于逐行比较文件的不同

diff [选项] 文件1 文件2

文件1如何变更,就能变成文件2

[root@MissHou ~]# cat file1
aaaa
111
hello world
222
333
bbb
[root@MissHou ~]#
[root@MissHou ~]# cat file2
aaa
hello
111
222
bbb
333
world

正常模式比较

diff目的:file1如何改变才能和file2匹配
[root@MissHou ~]# diff file1 file2
1c1,2					第一个文件的第1行需要改变(c=change)才能和第二个文件的第1到2行匹配			
< aaaa				小于号"<"表示左边文件(file1)文件内容
---					---表示分隔符
> aaa					大于号">"表示右边文件(file2)文件内容
> hello
3d3					第一个文件的第3行删除(d=delete)后才能和第二个文件的第3行匹配
< hello world
5d4					第一个文件的第5行删除后才能和第二个文件的第4行匹配
< 333
6a6,7					第一个文件的第6行增加(a=add)内容后才能和第二个文件的第6到7行匹配
> 333					需要增加的内容在第二个文件里是333和world
> world

上下文格式显示

[root@MissHou ~]# diff -c file1 file2
前两行主要列出需要比较的文件名和文件的时间戳;文件名前面的符号***表示file1,---表示file2
*** file1       2019-04-16 16:26:05.748650262 +0800
--- file2       2019-04-16 16:26:30.470646030 +0800
***************	我是分隔符
*** 1,6 ****		以***开头表示file1文件,1,6表示1到6行
! aaaa				!表示该行需要修改才与第二个文件匹配
  111
- hello world		-表示需要删除该行才与第二个文件匹配
  222
- 333					-表示需要删除该行才与第二个文件匹配
  bbb
--- 1,7 ----		以---开头表示file2文件,1,7表示1到7行
! aaa					表示第一个文件需要修改才与第二个文件匹配
! hello				表示第一个文件需要修改才与第二个文件匹配
  111
  222
  bbb
+ 333					表示第一个文件需要加上该行才与第二个文件匹配
+ world				表示第一个文件需要加上该行才与第二个文件匹配

合并格式显示

[root@MissHou ~]# diff -u file1 file2
前两行主要列出需要比较的文件名和文件的时间戳;文件名前面的符号---表示file1,+++表示file2
--- file1       2019-04-16 16:26:05.748650262 +0800
+++ file2       2019-04-16 16:26:30.470646030 +0800
@@ -1,6 +1,7 @@
-aaaa
+aaa
+hello
 111
-hello world
 222
-333
 bbb
+333
+world

比较两个目录中的文件

默认情况下也会比较两个目录里相同文件的内容
[root@MissHou  tmp]# diff dir1 dir2
diff dir1/file1 dir2/file1
0a1
> hello
Only in dir1: file3
Only in dir2: test1
如果只需要比较两个目录里文件的不同,不需要进一步比较文件内容,需要加-q选项
[root@MissHou  tmp]# diff -q dir1 dir2
Files dir1/file1 and dir2/file1 differ
Only in dir1: file3
Only in dir2: test1

7.paste—文件内容合并

常用选项:
-d:自定义间隔符,默认是tab
-s:串行处理,非并行

[root@node2 shell_study]# paste file1 file2
aaaa    aaa
111     hello
hello world     111
222     222
333     bbb
bbb     333
        world
[root@node2 shell_study]# paste -d: file1 file2
aaaa:aaa
111:hello
hello world:111
222:222
333:bbb
bbb:333
:world
[root@node2 shell_study]# paste -s file1 file2
aaaa    111     hello world     222     333     bbb
aaa     hello   111     222     bbb     333     world
[root@node2 shell_study]# 
[root@node2 shell_study]# cat file1 file2
aaaa
111
hello world
222
333
bbb
aaa
hello
111
222
bbb
333
world
[root@node2 shell_study]# paste file1 file2
aaaa    aaa
111     hello
hello world     111
222     222
333     bbb
bbb     333
        world

8. tr工具—转换替换

tr用于字符转换,替换和删除

用法1:命令的执行结果交给tr处理,其中string1用于查询,string2用于转换处理

 commands|tr  'string1'  'string2'

用法2:tr处理的内容来自文件,记住要使用"<"标准输入

 tr  'string1'  'string2' < filename

用法3:匹配string1进行相应操作,如删除操作

 tr [options] 'string1' < filename

-d 删除字符串1中所有输入字符。
-s 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串

在这里插入图片描述
在这里插入图片描述
如上方式:set list 就可以显示出文档的制表符了

将 :或者/替换为#

[root@node2 shell_study]# tr ':/' '#' <1.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
lp#x#4#7#lp##var#spool#lpd##sbin#nologin
sync#x#5#0#sync##sbin##bin#sync
shutdown#x#6#0#shutdown##sbin##sbin#shutdown
halt#x#7#0#halt##sbin##sbin#halt
mail#x#8#12#mail##var#spool#mail##sbin#nologin
operator#x#11#0#operator##root##sbin#nologin
[root@node2 shell_study]# 

tr ' ;/' '@' <1.txt 会将1.txt作为内容输入,tr处理1.txt中的内容
并且替换的时候,是用@替换空格,用@替换;,用@替换/,而不会把 ‘空格;/’ 作为整体替换为一个@

练习 删除小写字母

[root@node2 shell_study]# tr -d 'a-z' <1.txt
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
::5:0::/://
::6:0::/://
::7:0::/://
::8:12::///://
::11:0::/://
[root@node2 shell_study]# 

删除除了字母以外的所有符号

# tr -d '[:/]' < 3.txt 				删除文件中的:和/
# cat 3.txt |tr -d '[:/]'			删除文件中的:和/
# tr '[0-9]' '@' < 3.txt 			将文件中的数字替换为@符号
# tr '[a-z]' '[A-Z]' < 3.txt 		将文件中的小写字母替换成大写字母
# tr -s '[a-z]' < 3.txt 			匹配小写字母并将重复的压缩为一个
# tr -s '[a-z0-9]' < 3.txt 		匹配小写字母和数字并将重复的压缩为一个
# tr -d '[:digit:]' < 3.txt 		删除文件中的数字
# tr -d '[:blank:]' < 3.txt 		删除水平空白
# tr -d '[:space:]' < 3.txt 		删除所有水平和垂直空白

综合练习

使用小工具分别截取当前主机IP;截取NETMASK;截取广播地址;截取MAC地址

分别是ip地址,广播地址,和子网掩码

ifconfig enp0s3

[root@node2 shell_study]# ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.218.26  netmask 255.255.255.0  broadcast 10.1.218.255
        inet6 fe80::898c:83a4:74f0:e4c8  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::7fc4:e393:bddd:40e5  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:f1:33:6b  txqueuelen 1000  (Ethernet)
        RX packets 629464  bytes 67981767 (64.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 815577  bytes 144514631 (137.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



[root@node2 shell_study]# ifconfig enp0s3|grep netmask |cut -d ' ' -f10,13,16|tr ' ' '\n'
10.1.218.26
255.255.255.0
10.1.218.255

2.bash 特性

^c   			终止前台运行的程序
^z	  			将前台运行的程序挂起到后台
^d   			退出 等价exit
^l   			清屏 
^a |home  	光标移到命令行的最前端
^e |end  	光标移到命令行的后端
^u   			删除光标前所有字符
^k   			删除光标后所有字符
^r	 			搜索历史命令

ctrl+r 进入历史搜索界面,输入关键字,就可以匹配历史命令了

*:	匹配0或多个任意字符
?:	匹配任意单个字符
[list]:	匹配[list]中的任意单个字符,或者一组单个字符   [a-z]
[!list]: 匹配除list中的任意单个字符
{string1,string2,...}:匹配string1,string2或更多字符串


# rm -f file*
# cp *.conf  /dir1
# touch file{1..5}
[root@node2 shell_study]# touch file{1..4}.jpg
[root@node2 shell_study]# ls -l file*.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file3.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file4.jpg
[root@node2 shell_study]# 



[root@node2 shell_study]# ls -l *.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file3.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file4.jpg
[root@node2 shell_study]# ls -l file?.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file3.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file4.jpg
[root@node2 shell_study]# ls -l file??.jpg
ls: 无法访问file??.jpg: 没有那个文件或目录
[root@node2 shell_study]# ls -l [file].jpg
ls: 无法访问[file].jpg: 没有那个文件或目录
[root@node2 shell_study]# ls -l [a-z].jpg
ls: 无法访问[a-z].jpg: 没有那个文件或目录
[root@node2 shell_study]# ls -l file{1..2}.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg


[root@node2 shell_study]# ls -l file[0-9].jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file3.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file4.jpg
[root@node2 shell_study]# ls -l file[!0-9].jpg
ls: 无法访问file[!0-9].jpg: 没有那个文件或目录
[root@node2 shell_study]# ls -l file[!a-z].jpg
-rw-r--r--. 1 root root 0 1018 18:30 file1.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file2.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file3.jpg
-rw-r--r--. 1 root root 0 1018 18:30 file4.jpg
[root@node2 shell_study]# 



2. bash中的引号

双引号"" :会把引号的内容当成整体来看待,允许通过$符号引用其他变量值

单引号’’ :会把引号的内容当成整体来看待,禁止引用其他变量值,shell中特殊符号都被视为普通字符

反撇号`` :反撇号和$()一样,引号或括号里的命令会优先执行,如果存在嵌套,反撇号不能用

[root@node2 shell_study]# echo $(hostname)
node2
[root@node2 shell_study]# date +%F
2022-10-18

[root@node2 shell_study]# echo "hello world"
hello world
[root@node2 shell_study]# echo 'hello world'
hello world
[root@node2 shell_study]# echo `hello world`
-bash: hello: 未找到命令

[root@node2 shell_study]# echo "$(hostname)"
node2
[root@node2 shell_study]# echo '$(hostname)'
$(hostname)


[root@node2 shell_study]# date +%F
2022-10-18
[root@node2 shell_study]# echo $(date +%F)
2022-10-18
[root@node2 shell_study]# echo '$(date +%F)'
$(date +%F)
[root@node2 shell_study]# echo "$(date +%F)"
2022-10-18
[root@node2 shell_study]# echo `date +%F`
2022-10-18

[root@MissHou  dir1]# echo $(date +%F)
2018-11-22
[root@MissHou  dir1]# echo `echo $(date +%F)`
2018-11-22
[root@MissHou  dir1]# echo `date +%F`
2018-11-22
[root@MissHou  dir1]# echo `echo `date +%F``
date +%F
[root@MissHou  dir1]# echo $(echo `date +%F`)
2018-11-22

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值