shell特殊符号cut命令
1、* 任意个任意字符(这是一个通配符)
2、? 任意一个字符
3、# 注释字符
4、\ 脱义字符
[root@xietaolinux3 ~]# a=1
[root@xietaolinux3 ~]# b=2
[root@xietaolinux3 ~]# c=$a$b
[root@xietaolinux3 ~]# echo $c
12
[root@xietaolinux3 ~]# c=\$a\$b
[root@xietaolinux3 ~]# echo $c
$a$b
5、| 管道符
cut命令(用来截取某一个字段字符串)
格式 cut -d ‘分隔符’ [-cf] n
这里的n是数字。该命令有如下几个选项:
-d:后面跟分隔符,分隔字符要用单引号括起来
-f:后面接的是第几个字符
-c:后面接的是第几个区块
使用/etc/passwd文件示例
[root@aminglinux-01 ~]# cat /etc/passwd |head
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@aminglinux-01 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
#针对前两行做一个截取,截取第一段
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
#截取第一段和第二段
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
#截取第一段和第三段
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
#截取第四个字符
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -c 4
t
:
#-c选项后面可以是1个数字n,也可以是一个区间n1-n2,还可以是多个数字n1,n2和n3
[root@localhost ~]# cat /etc/passwd |head -2 |cut -c 1,3,10
ro0
bn:
sort_wc_uniq命令
sort命令
sort 命令是用做排序,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中获取输入。
其格式为sort [-t 分隔符] [-kn1,n2] [-nru],这里的n1和n2指的是数字,其他选项的含义如下:
-n :表示使用纯数字排序,字母与特殊符号会识别为0
-r :表示反向排序
-t :后面跟分隔字符,作用跟cut的-d选项一样
-kn1/-kn1,n2:表示由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序。
使用/etc/passwd示例如下
#按照ID号顺序排序,数字、字母从小到大排序)
root@aminglinux-01 ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
aminglinux:x:1002:1002::/home/aminglinux:/bin/bash
aming:x:1000:1005::/home/aming:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
#head一个文件的前10追加了1.txt
[root@xietaolinux3 ~]# head /etc/passwd > 1.txt
#按阿斯玛ID顺序排序
[root@xietaolinux3 ~]# sort 1.txt
<
>
[
]
{
1.txt
22222aaaa
2.txt
333222
3443333
455555
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
*gafgsg
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
#以数字排序,字母与特殊符号会识别为0
[root@localhost ~]# sort -n 1.txt
<
>
[
{
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
*gfdgsdfgsgffff
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
1.txt
2aaaa
2.txt
23333
23333331
4888888888888888adgaga
2222222222222222222aaaaaaaaa
wc命令
wc命令用来计算数字。利用wc指令我们可以计算文件的Byte数、字数或是列数,若不指定文件名称,或是所给予的文件名为“-”,则wc指令会从标准输入设备读取数据。
wc命令用统计文档的行数、字符数或词数。
-l 统计行数
-m 统计字符数
-w 统计词数
示例
#统计行数
[root@aminglinux-01 ~]# wc -l 1.txt
22 1.txt
#统计字符数
[root@aminglinux-01 ~]# wc -m 1.txt
450 1.txt
#重新新建一个文件
[root@localhost ~]# vim 2.txt
[root@localhost ~]# cat 2.txt
123
abc
#为什么是8个而不是6个?因为每一行最后面都有一个换行符
[root@localhost ~]# wc -m 2.txt
8 2.txt
#-A查看文件所以东西,包括隐藏
[root@localhost ~]# cat -A 2.txt
123$
abc$
[root@localhost ~]# cat 2.txt
123
abc 1111,222 3333
#加-w为统计词, 以空格来区分,哪怕你写一个逗号,也会认为是一整个词。
[root@localhost ~]# wc -w 2.txt /
4 2.txt
uniq命令
uniq命令用来删除重复的行,一般与sort命令结合使用。该命令只有-c选项比较常用 ,它表示统计重复的行数,并把行数写在前面。
单独使用uniq命令去重,只有在列表相同的上下序列才去重,
选项
-c或——count:在每列旁边显示该行重复出现的次数;
-d或--repeated:仅显示重复出现的行列;
-f<栏位>或--skip-fields=<栏位>:忽略比较指定的栏位;
-s<字符位置>或--skip-chars=<字符位置>:忽略比较指定的字符;
-u或——unique:仅显示出一次的行列;
-w<字符位置>或--check-chars=<字符位置>:指定要比较的字符。
uniq示例
[root@aminglinux-01 ~]# vim 2.txt
[root@aminglinux-01 ~]# cat 2.txt
123
abc 111,222
123
abc
1
2
1
#没有任何变化,没有序列相同的,无法去重。
[root@aminglinux-01 ~]# uniq 2.txt
123
abc 111,222
123
abc
1
2
1
#更改数字序列
[root@aminglinux-01 ~]# vim 2.txt
[root@aminglinux-01 ~]# cat 2.txt
123
abc 111,222
123
abc
1
1
2
#去重相同的数字1,但是123并没有去重,去重是需要先排序的
[root@aminglinux-01 ~]# uniq 2.txt
123
abc 111,222
123
abc
1
2
#结合sort命令使用先排序,然后去重,计算重复次数
#使用sort命令排序2.txt文件列表
[root@aminglinux-01 ~]# sort 2.txt
1
1
123
123
2
abc
abc 111,222
#加管道uniq去重
[root@aminglinux-01 ~]# sort 2.txt |uniq
1
123
2
abc
abc 111,222
#加-c计算统计重复次数 (重点)
[root@aminglinux-01 ~]# sort 2.txt |uniq -c
2 1
2 123
1 2
1 abc
1 abc 111,222
tee_tr_split命令
tee命令
tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin。简单的说就是把数据重定向到给定文件和屏幕上。
存在缓存机制,每1024个字节将输出一次。若从管道接收输入数据,应该是缓冲区满,才将数据转存到指定的文件中。若文件内容不到1024个字节,则接收完从标准输入设备读入的数据后,将刷新一次缓冲区,并转存数据到指定文件。
tee 命令后面跟文件名,其作用类似于重写向>,但它比重定向多一个功能,即把文件写入后面的所跟的文件时,还显示在屏幕上,该命令常用于管理符|后。
选项
-a:向文件中重定向时使用追加模式;
-i:忽略中断(interrupt)信号。
tee示例
#把一个文件输出结果重定向到a.txt里,并把结果输出在屏幕上
[root@aminglinux-01 ~]# sort 2.txt |uniq -c |tee a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
#清空内容,重新来一遍
[root@aminglinux-01 ~]# >a.txt
[root@aminglinux-01 ~]# cat a.txt
[root@aminglinux-01 ~]# sort 2.txt |uniq -c |tee a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
#tee加-a是追加
[root@aminglinux-01 ~]# sort 2.txt |uniq -c |tee -a a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
2 1
2 123
1 2
1 abc
1 abc 111,222
tr命令
tr命令可以对来自标准输入的字符进行替换、压缩和删除。它可以将一组字符变成另一组字符,经常用来编写优美的单行命令,作用很强大。
tr 命令用于替换字符,常用来处理文档中出现的特殊符号,如DOS文档中出现的符号^m。
该命令常用 的选项有以下两个
-d:表示删除某个字符,后面跟要删除的字符。
-s:表示删除重复的字符。
示例
#方括号的意思是任选一个,替换前面方括号里的字符
[root@aminglinux-01 ~]# echo "aminglinux" |tr '[al]' '[AL]'
AmingLinux
#只替换一个字符
[root@xietaolinux3 ~]# echo "aminglinux" |tr 'a' 'A'
Aminglinux
#替换所有的字符
[root@aminglinux-01 ~]# echo "aminglinux" |tr '[a-z]' '[A-Z]'
AMINGLINUX
#把所有的字母替换成1,后面不需要方括号
[root@aminglinux-01 ~]# echo "aminglinux" |tr '[a-z]' '1'
1111111111
split命令
split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,比如为提高可读性,生成日志等。
比如系统日志没有做过切割,经过几个月后日志变成了四五百G,这么大的日志没法查看,只能把日志切割了五千份。
split 命令用于切割(切割日志,两种用法)
-b:表示依据大小来分割文档(默认单位byte字节)
-l:表示依据行数来分割文档
使用命令:
split -b 100m bigfile #切割文件大小
split -l 1000 bigfile #切割文件行数
示例:
#把etc下面的以conf结尾的文件全部列出来
[root@ataolinux-01 ~]# find /etc/ -type f -name "*conf"
#并把所有文件的内容cat出来并追加重定义到a.txt文件,去脱义。
[root@ataolinux-01 ~]# find /etc/ -type f -name "*conf" -exec cat {} >> a.txt \;
#查看文件大小
[root@ataolinux-01 ~]# du -sh a.txt
252K a.txt
#创建新的目录,把a.txt放到新目录下,以防止混乱
[root@ataolinux-01 ~]# mkdir test
[root@ataolinux-01 ~]# mv a.txt test/
[root@ataolinux-01 ~]# cd test/
[root@aminglinux-01 test]# ls
a.txt
#给a.txt文件切割成1000
[root@ataolinux-01 test]# split -b 1000 a.txt
[root@ataolinux-01 test]# ls
a.txt xam xaz xbm xbz xcm xcz xdm xdz xem xez xfm xfz xgm xgz xhm xhz xim xiz xjm
xaa xan xba xbn xca xcn xda xdn xea xen xfa xfn xga xgn xha xhn xia xin xja xjn
xab xao xbb xbo xcb xco xdb xdo xeb xeo xfb xfo xgb xgo xhb xho xib xio xjb xjo
xac xap xbc xbp xcc xcp xdc xdp xec xep xfc xfp xgc xgp xhc xhp xic xip xjc xjp
xad xaq xbd xbq xcd xcq xdd xdq xed xeq xfd xfq xgd xgq xhd xhq xid xiq xjd xjq
xae xar xbe xbr xce xcr xde xdr xee xer xfe xfr xge xgr xhe xhr xie xir xje xjr
xaf xas xbf xbs xcf xcs xdf xds xef xes xff xfs xgf xgs xhf xhs xif xis xjf xjs
xag xat xbg xbt xcg xct xdg xdt xeg xet xfg xft xgg xgt xhg xht xig xit xjg xjt
xah xau xbh xbu xch xcu xdh xdu xeh xeu xfh xfu xgh xgu xhh xhu xih xiu xjh xju
xai xav xbi xbv xci xcv xdi xdv xei xev xfi xfv xgi xgv xhi xhv xii xiv xji xjv
xaj xaw xbj xbw xcj xcw xdj xdw xej xew xfj xfw xgj xgw xhj xhw xij xiw xjj xjw
xak xax xbk xbx xck xcx xdk xdx xek xex xfk xfx xgk xgx xhk xhx xik xix xjk
xal xay xbl xby xcl xcy xdl xdy xel xey xfl xfy xgl xgy xhl xhy xil xiy xjl
#查看文件大小
[root@ataolinux-01 test]# du -sh
1.3M .
[root@xietaolinux3 test]# du -sh *
320K a.txt
4.0K xaa
4.0K xab
4.0K xac
4.0K xad
4.0K xae
4.0K xaf
4.0K xag
4.0K xah
4.0K xai
4.0K xaj
4.0K xak
#单位B
[root@xietaolinux3 test]# du -sb *
325146 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
1000 xag
1000 xah
1000 xai
1000 xaj
#再往下切割,会增加文件名的长度
#使用rm -f x*删除掉前面切割的文件,再指定文件为100K
[root@aminglinux-01 test]# split -b 100k a.txt
[root@aminglinux-01 test]# ls
a.txt xaa xab xac
[root@aminglinux-01 test]# du -sh *
256K a.txt
100K xaa
100K xab
52K xac
#全部删除
[root@aminglinux-01 test]# rm -f x*
#切割指定大小的同时也指定名字
[root@aminglinux-01 test]# split -b 100k a.txt abc
[root@aminglinux-01 test]# ls
abcaa abcab abcac a.txt
[root@aminglinux-01 test]# split -b 100k a.txt abc.
[root@aminglinux-01 test]# ls
abcaa abc.aa abcab abc.ab abcac abc.ac a.txt
[root@aminglinux-01 test]# rm -f abc*
#指定行
[root@aminglinux-01 test]# split -l 1000 a.txt
[root@aminglinux-01 test]# ls -l
总用量 516
-rw-r--r--. 1 root root 257421 11月 17 20:00 a.txt
-rw-r--r--. 1 root root 42094 11月 17 20:04 xaa
-rw-r--r--. 1 root root 44424 11月 17 20:04 xab
-rw-r--r--. 1 root root 40244 11月 17 20:04 xac
-rw-r--r--. 1 root root 40202 11月 17 20:04 xad
-rw-r--r--. 1 root root 34871 11月 17 20:04 xae
-rw-r--r--. 1 root root 39403 11月 17 20:04 xaf
-rw-r--r--. 1 root root 16183 11月 17 20:04 xag
[root@aminglinux-01 test]# wc -l *
6495 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
495 xag
12990 总用量
shell特殊符号下
特殊符号$
符号$可以用作变量前面的标识符,还可以和!$组合起来使用,正则里面表示行尾,示例命令如下:
[root@localhost ~]# ls test.txt
test.txt
[root@localhost ~]# ls !$
ls test.txt
test.txt
!$表示上条命令中的最后一个变量,本例中上条命令最后是test.txt,那么在当前命令下输入!$则代表test.txt
特殊符号;
多条命令写到一行,用分号分割,示例命令如下:
[root@aminglinux-01 ~]# ls 1.txt ; wc -l 2.txt
1.txt
7 2.txt
#中间不需要空格也是可以的
[root@xietaolinux3 ~]# ls 1.txt;wc -l 2.txt
1.txt
7 2.txt
特殊符号~
符号 ~ 表示用户的家目录,root用户的家目录是/root,普通用户则是/home/username。后面正则表达式表示匹配符
[root@localhost ~]# cd ~
[root@localhost ~]# pwd
/root
#SU切换一个用户
[root@localhost ~]# su aming
[aming@localhost root]$ cd ~
[aming@localhost ~]$ pwd
/home/aming
特殊符号&
如果想把一条命令放到命令后台执行,则需要加上符号&,它通常用于命令运行时间较长的情况。比如可以用在sleep后,如下所示:
#把一条命令丢到后台去
[root@localhost ~]# sleep 30 &
[1] 4176
#查看进程列表
[root@localhost ~]# jobs
[1]+ 运行中 sleep 30 &
重定向符号 >,>>, 2> ,2>> 和&>
> :正确重定向,会把之前的文件覆盖掉。
>> :追加重定向,追加内容正确输出。
2> :错误重定向
2>> :追加错误重定向
&> :表示正确与错误输出重定向
前面讲过重定向符号>和>>,它们分别表示取代和追加的意思。当我们运行一个命令报错时,报错信息会输出到当前屏幕。如果想重定向到一个文本,则要用重定向符号2>或者2>>,它们分别表示错误重定向和错误追加重定向。示例命令如下:
[root@localhost ~]# ls aaaa
ls: 无法访问aaaa: 没有那个文件或目录
#把错误的结果重定向到一条命令里去
[root@localhost ~]# ls aaaa 2> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 无法访问aaaa: 没有那个文件或目录
#把错误的结果追加重定向到一条命令里去
[root@localhost ~]# ls aaaa 2>> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 无法访问aaaa: 没有那个文件或目录
ls: 无法访问aaaa: 没有那个文件或目录
中括号[ ]
中括号内为字符组合,代表字符组合中的任意一个,可以是一个范围(1-3,a-z),如[0-9],[a-zA-Z],[abc]
[root@localhost ~]# cd /tmp/10/
[root@localhost 10]# touch test1.txt test2.txt testb.txt testdir
[root@localhost 10]# ls
1.txt test1.txt test2.txt testb.txt testdir
[root@localhost 10]ls -d test*
test1.txt test2.txt testb.txt testdir
[root@localhost 10]# ls -d test[1-3].txt
test1.txt test2.txt
[root@localhost 10]# ls -d test[12b].txt
test1.txt test2.txt testb.txt
[root@localhost 10]# ls -d test[1-9].txt
test1.txt test2.txt
[root@localhost 10]# ls -d test[1-9a-z].txt
test1.txt test2.txt testb.txt
特殊符号|| 和 && ,用于命令之间
|| 用于shell中表示或者的意思,当同时使用执行两条命令时,如果第一条命令执行不成功,那就执行第二条命令,如果第一条命令执行成功,那就不执行第二条命令。
#分号执行两条命令
[root@aminglinux-01 ~]# ls 1a.txt ; wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
7 2.txt
#中间增加||,第一条命令执行不成功,执行第二条
[root@aminglinux-01 ~]# ls 1a.txt || wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
7 2.txt
#中间增加||,第一条命令执行成功,不执行第二条
[root@aminglinux-01 ~]# ls 1.txt || wc -l 2.txt
1.txt
&&当同时使用执行两条命令时,如果前面命令执行成功才执行后面的命令,否则不执行。
#增加&&,第一条命令执行成功,再执行第二条命令
[root@aminglinux-01 ~]# ls 1.txt && wc -l 2.txt
1.txt
7 2.txt
#增加&&,第一条命令执行不成功,结束,第二条命令不执行
[root@aminglinux-01 ~]# ls 1a.txt && wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
实际场景实验示例:
当想创建一个aminglinux目录,但是有一个条件,如果这个目录存在就不创建,不存在就创建。
[root@aminglinux-01 ~]# ls
111 1_heard.txt.bak 1.txt 234 3.txt aa.txt anaconda-ks.cfg test
123 1_sorft.txt.bak 1.txt.bak 2.txt 456 aming2 bb.txt 安诺云智平台介绍(PPT模板).pptx
#判断一个目录是否存在,如果目录不存在就创建它
[root@aminglinux-01 ~]# [ -d aminglinux ] || mkdir aminglinux
[root@aminglinux-01 ~]# ls
111 1_heard.txt.bak 1.txt 234 3.txt aa.txt aminglinux bb.txt 安诺云智平台介绍(PPT模板).pptx
123 1_sorft.txt.bak 1.txt.bak 2.txt 456 aming2 anaconda-ks.cfg test
#判断一个目录是否存在,如果目录存在就不创建
[root@aminglinux-01 ~]# [ -d aminglinux ] && mkdir aminglinux
mkdir: 无法创建目录"aminglinux": 文件已存在
解释:
[ -d aminglinux ]:判断一个目录是否存在。