前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux基础(day25)

linux基础(day25)

作者头像
运维小白
发布2022-01-06 13:51:13
3590
发布2022-01-06 13:51:13
举报
文章被收录于专栏:运维小白运维小白

8.10 shell特殊符号cut命令

特殊符号

代码语言:javascript
复制
* 通配符,任意个任意字符
? 任意一个字符
# 注释字符,在命令或脚本前面写入加#号,就表示这一行不会生效
\ 脱义字符,
| 管道符

cut命令

  • cut命令,截取字符串,显示行中的指定部分,删除文件中指定字段
    • -d 分隔符
    • -f 指定段号,若是指定多段字符的时候,可以用- 或, 表示
      • 比如 -f 1,2 或 -f 1-3
    • -c 指定第几个字符
      • 在使用 -c 参数后,就不要使用 -d 和 -f 参数了
代码语言:javascript
复制
[root@hf-01 ~]# cat /etc/passwd |head -2        //查看文件的前两行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1        //截取文件前两行中以冒号作为分割符的第一段
root
bin
[root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@hf-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
[root@hf-01 ~]# cat /etc/passwd |head -2 |cut -c 4        //截取两段文件前两行中显示第4个字符
t
:

8.11 sort_wc_uniq命令

sort命令

  • sort 命令,排序。将文件进行排序,并将排序结果标准输出
    • sort命令,默认按照ASCII码排序
    • -n 以数字排序,而其中的字符和字母都会默认为0
    • -r 反序
    • -t 分隔符
    • -kn1/-kn1,n2
代码语言:javascript
复制
[root@hf-01 ~]# sort /etc/passwd        //sort命令,默认按照ASCII码排序
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
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@hf-01 ~]# head /etc/passwd >> 1.txt        //head命令,默认显示前十行
[root@hf-01 ~]# vim 1.txt        //并在文件中添加一些字符,特殊符号
[root@hf-01 ~]# sort 1.txt        //sort命令,默认按照ASCII码排序
<
{
1.txt
222111
22222222222aaaaaa
223333
22aaa
2.txt
47888888gdkgljsd
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
*dffadg
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
[root@hf-01 ~]# 
sort命令的参数 -n
  • sort -n 1.txt中的字母和字符都默认为0
    • 所以字母和符号会排在最前面,然后是数字排序
代码语言:javascript
复制
[root@hf-01 ~]# 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
*dffadg
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
1.txt
2.txt
22aaa
222111
223333
47888888gdkgljsd
22222222222aaaaaa
[root@hf-01 ~]# 
sort命令的参数-r
  • sort -nr 1.txt //反序排序
    • -r表示反序
代码语言:javascript
复制
[root@hf-01 ~]# sort -nr 1.txt
22222222222aaaaaa
47888888gdkgljsd
223333
222111
22aaa
2.txt
1.txt
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
*dffadg
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
{
<
[root@hf-01 ~]# 
sort命令的参数 -t
  • sort -t 分隔符

wc命令

  • wc -l 统计行数
    • -m 统计字符数 ,也会统计换行符(换行符是隐藏的)
    • -w 统计词,它是以空格、空白字符进行区分的
      • 以分号分割的也会认为是一个字符,比如111,qqq这个就会认为是一个字符
  • cat -A 显示文件中所有的字符(包括隐藏的)
代码语言:javascript
复制
[root@hf-01 ~]# wc -l 1.txt    //显示文件的行数
22 1.txt
[root@hf-01 ~]# wc -m 1.txt        //显示文件中的字符数
468 1.txt
[root@hf-01 ~]# vim 2.txt        //在文件中编写两行,6个字符
[root@hf-01 ~]# wc -m 2.txt    //在查看的时候,会显示出8个字符
8 2.txt
[root@hf-01 ~]# cat -A 2.txt        //会统计所有的字符,包括隐藏字符
123$
avd$
[root@hf-01 ~]# wc -w 2.txt        //它是以空格、空白字符进行区分的
2 2.txt

uniq命令

  • uniq 去重, 用于报告或忽略文件中的重复行。常与sort排序命令结合使用
    • -c统计行数

uniq命令去重条件:需要先排序,再去重

代码语言:javascript
复制
[root@hf-01 ~]# vim 2.txt
[root@hf-01 ~]# cat 2.txt
123
avd 112,21a
123
avd
1
2
1
[root@hf-01 ~]# uniq 2.txt
123
avd 112,21a
123
avd
1
2
1
[root@hf-01 ~]# vim 2.txt
[root@hf-01 ~]# cat 2.txt        //查看文件内容
123
avd 112,21a
123
avd
1
1
2
[root@hf-01 ~]# uniq 2.txt        //会看到在更改排序后,去重了
123
avd 112,21a
123
avd
1
2
[root@hf-01 ~]# 
  • uniq命令和sort命令结合使用
    • 先排序,再去重
代码语言:javascript
复制
[root@hf-01 ~]# sort 2.txt
1
1
123
123
2
avd
avd 112,21a
[root@hf-01 ~]# sort 2.txt |uniq
1
123
2
avd
avd 112,21a
[root@hf-01 ~]# 
uniq命令参数 -c
  • uniq -c 统计重复次数
代码语言:javascript
复制
[root@hf-01 ~]# sort 2.txt |uniq -c
      2 1
      2 123
      1 2
      1 avd
      1 avd 112,21a
[root@hf-01 ~]# 

8.12 tee_tr_split命令

tee命令

  • 清空文件内容 >1.txt
代码语言:javascript
复制
[root@hf-01 ~]# > 2.txt
[root@hf-01 ~]# cat 2.txt
[root@hf-01 ~]# 
  • tee命令和输出重定向>类似,重定向的同时还在屏幕显示
    • tee命令,就是重定向,把前面命令输出的结果打印到屏幕上
    • -a参数,追加重定向
代码语言:javascript
复制
[root@hf-01 ~]# cat 3.txt
asda
123 fgdg,45
1
abc
cda
abc
1
[root@hf-01 ~]# sort 3.txt |uniq -c |tee a.txt
      2 1
      1 123 fgdg,45
      2 abc
      1 asda
      1 cda
[root@hf-01 ~]# cat a.txt
      2 1
      1 123 fgdg,45
      2 abc
      1 asda
      1 cda
[root@hf-01 ~]# 
tee命令参数-a
  • tee -a 就是追加重定向
代码语言:javascript
复制
[root@hf-01 ~]# sort 3.txt |uniq -c |tee -a a.txt
      2 1
      1 123 fgdg,45
      2 abc
      1 asda
      1 cda
[root@hf-01 ~]# cat a.txt
      2 1
      1 123 fgdg,45
      2 abc
      1 asda
      1 cda
      2 1
      1 123 fgdg,45
      2 abc
      1 asda
      1 cda
[root@hf-01 ~]# 

tr命令

  • tr 命令,用来替换字符的命令,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'
    • 支持写多个字符替换
代码语言:javascript
复制
[root@hf-01 ~]# echo "hanfeng" |tr '[hf]' '[HF]'
HanFeng
[root@hf-01 ~]# echo "hanfeng" |tr 'h' 'H'
Hanfeng
[root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '[A-Z]'
HANFENG
[root@hf-01 ~]# 
  • 字符替换数字的时候,需要注意格式
    • 替换数字的时候,需要去除方括号[]
代码语言:javascript
复制
[root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '[1]'    //错误示范,这样写会出错
]1]]]]]
[root@hf-01 ~]# echo "hanfeng" |tr '[a-z]' '1'        //在替换成数字的时候,需要去除方括号
1111111
[root@hf-01 ~]# 

split命令

  • split 切割,将一个大文件切割成很多个小文件
    • -b大小(默认单位字节)
      • 格式:split -b 100M bigfile
        • 若不写单位,会默认是字节
    • -l行数
      • 格式:split -l 1000 bigfile
代码语言:javascript
复制
[root@hf-01 ~]# find /etc/ -type f -name "*conf" -exec cat {} >>a.txt \;    //将etc目录下所有文件以conf结尾的文件全都输出重定向到a.txt文件中
[root@hf-01 ~]# du -sh a.txt
252K	a.txt
[root@hf-01 ~]# mv a.txt 111/    //把a.txt文件移动到111目录下
[root@hf-01 ~]# cd 111/            //切换到111目录下
[root@hf-01 111]# ls
a.txt
[root@hf-01 111]# split -b 1000 a.txt    //单位是字节(1000byte=1k)
[root@hf-01 111]# ls
a.txt  xbe  xcj  xdo  xet  xfy  xhd  xii  xjn  xks  xlx  xnc  xoh  xpm  xqr  xrw  xtb
xaa    xbf  xck  xdp  xeu  xfz  xhe  xij  xjo  xkt  xly  xnd  xoi  xpn  xqs  xrx  xtc
xab    xbg  xcl  xdq  xev  xga  xhf  xik  xjp  xku  xlz  xne  xoj  xpo  xqt  xry  xtd
xac    xbh  xcm  xdr  xew  xgb  xhg  xil  xjq  xkv  xma  xnf  xok  xpp  xqu  xrz  xte
等等等,只截取了一小部分
[root@hf-01 111]# du -sh        //查看目录下文件大小
2.3M	.
[root@hf-01 111]# du -sh *        //会看到分割出来的都占据了一个块
252K	a.txt
4.0K	xaa
4.0K	xab
4.0K	xac
4.0K	xad
等等等,只截取了一小部分
[root@hf-01 111]# rm -f x*

在切割一个文件,在不指定任何的文件名,最后显示的切割完的文件,会是已xab,xac这样一直延续下去,若再切割一次,则会zxaaa,zxaab等依次下去

  • split -b指定单位大小切割
代码语言:javascript
复制
[root@hf-01 111]# 
[root@hf-01 111]# split -b 100k a.txt
[root@hf-01 111]# ls
a.txt  xaa  xab  xac
[root@hf-01 111]# du -sh *
252K	a.txt
100K	xaa
100K	xab
52K	xac
[root@hf-01 111]# rm -f x*
[root@hf-01 111]# 
  • 指定文件大小的同时,指定文件的名称
代码语言:javascript
复制
[root@hf-01 111]# split -b 100k a.txt abc
[root@hf-01 111]# ls
abcaa  abcab  abcac  a.txt
[root@hf-01 111]# split -b 100k a.txt abc.
[root@hf-01 111]# ls
abcaa  abc.aa  abcab  abc.ab  abcac  abc.ac  a.txt
[root@hf-01 111]# rm -f abc*

切割的文件默认是以x开头!!!

split命令的参数-l

  • split -l 指定行数
代码语言:javascript
复制
[root@hf-01 111]# split -l 1000 a.txt
[root@hf-01 111]# ls -l
总用量 512
-rw-r--r--. 1 root root 256144 11月 18 06:41 a.txt
-rw-r--r--. 1 root root  44741 11月 18 06:59 xaa
-rw-r--r--. 1 root root  44239 11月 18 06:59 xab
-rw-r--r--. 1 root root  44320 11月 18 06:59 xac
-rw-r--r--. 1 root root  34153 11月 18 06:59 xad
-rw-r--r--. 1 root root  38618 11月 18 06:59 xae
-rw-r--r--. 1 root root  34693 11月 18 06:59 xaf
-rw-r--r--. 1 root root  15380 11月 18 06:59 xag
[root@hf-01 111]# wc -l *
  6548 a.txt
  1000 xaa
  1000 xab
  1000 xac
  1000 xad
  1000 xae
  1000 xaf
   548 xag
 13096 总用量
[root@hf-01 111]# 

8.13 shell特殊符号下

特殊符号

代码语言:javascript
复制
$ 变量前缀,!$组合,正则里面表示行尾
;多条命令写到一行,用分号分割 
~ 用户家目录,后面正则表达式表示匹配符
& 放到命令后面,会把命令丢到后台
>    正确重定向
>>     追加重定向
2>    错误重定向
2>>    错误追加重定向
&>    正确和错误输出重定向
[ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc]
|| 和 && ,用于命令之间
  • || 表示 或者 的意思
    • 两条命令之间,第一条命令如果执行不成功,那就会执行第二条
      • 若第一条命令执行成功,那么就不会再执行第二条命令
  • && 表示 和 的意思
    • 两条命令之间,第一条命令执行成功后,才会执行第二条命令
判断一个目录是否存在
  • [ -d haha ]判断这个是否是一个目录,是否存在的
    • 如果是目录,并且存在,那 [ -d haha ] 执行成功
代码语言:javascript
复制
[root@hf-01 ~]# [ -d haha ] || mkdir haha
[root@hf-01 ~]# ls
111  1_heard.txt  1.txt  2.txt      3.txt      haha
[root@hf-01 ~]# [ -d haha ] && mkdir haha      
mkdir: 无法创建目录"haha": 文件已存在
[root@hf-01 ~]# 

相关测验题目

扩展

  1. source exec 区别
  2. Linux特殊符号大全
  3. sort并未按ASCII排序
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/11/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 8.10 shell特殊符号cut命令
    • 特殊符号
      • cut命令
      • 8.11 sort_wc_uniq命令
        • sort命令
          • wc命令
            • uniq命令
            • 8.12 tee_tr_split命令
              • tee命令
                • tr命令
                  • split命令
                    • split命令的参数-l
                    • 8.13 shell特殊符号下
                      • 特殊符号
                      • 相关测验题目
                      • 扩展
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档