复习day25

8.10 shell特殊符号cut命令

特殊符号

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

cut命令

  • cut命令,截取字符串,显示行中的指定部分,删除文件中指定字段
    • -d 分隔符
    • -f 指定段号,若是指定多段字符的时候,可以用- 或, 表示
      • 比如 -f 1,2 或 -f 1-3
    • -c 指定第几个字符
      • 在使用 -c 参数后,就不要使用 -d 和 -f 参数了
[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
[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
    • 所以字母和符号会排在最前面,然后是数字排序
[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表示反序
[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 显示文件中所有的字符(包括隐藏的)
[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命令去重条件:需要先排序,再去重

[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命令结合使用
    • 先排序,再去重
[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 统计重复次数
[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
[root@hf-01 ~]# > 2.txt
[root@hf-01 ~]# cat 2.txt
[root@hf-01 ~]# 
  • tee命令和输出重定向>类似,重定向的同时还在屏幕显示
    • tee命令,就是重定向,把前面命令输出的结果打印到屏幕上
    • -a参数,追加重定向
[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 就是追加重定向
[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]’
    • 支持写多个字符替换
[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 ~]# 
  • 字符替换数字的时候,需要注意格式
    • 替换数字的时候,需要去除方括号[]
[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
[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指定单位大小切割

[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]# 
  • 指定文件大小的同时,指定文件的名称
[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 指定行数
[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特殊符号下

特殊符号

$ 变量前缀,!$组合,正则里面表示行尾
;多条命令写到一行,用分号分割 
~ 用户家目录,后面正则表达式表示匹配符
& 放到命令后面,会把命令丢到后台
>    正确重定向
>>     追加重定向
2>    错误重定向
2>>    错误追加重定向
&>    正确和错误输出重定向
[ ] 指定字符中的一个,[0-9],[a-zA-Z],[abc]
|| 和 && ,用于命令之间
  • || 表示 或者 的意思
    • 两条命令之间,第一条命令如果执行不成功,那就会执行第二条
      • 若第一条命令执行成功,那么就不会再执行第二条命令
  • && 表示 和 的意思
    • 两条命令之间,第一条命令执行成功后,才会执行第二条命令
判断一个目录是否存在
  • [ -d haha ]判断这个是否是一个目录,是否存在的
    • 如果是目录,并且存在,那 [ -d haha ] 执行成功
[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排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值