热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

linux基础(day25)

2019独角兽企业重金招聘Python工程师标准8.10shell特殊符号cut命令特殊符号*通配符,任意个任意字符?任意一个字符#注释字符,

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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&#64;hf-01 ~]#

sort命令的参数 -n

  • sort -n 1.txt中的字母和字符都默认为0
    • 所以字母和符号会排在最前面&#xff0c;然后是数字排序

[root&#64;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&#64;hf-01 ~]#

sort命令的参数-r

  • sort -nr 1.txt //反序排序
    • -r表示反序

[root&#64;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&#64;hf-01 ~]#

sort命令的参数 -t

  • sort -t 分隔符

wc命令

  • wc -l 统计行数
    • -m 统计字符数 &#xff0c;也会统计换行符&#xff08;换行符是隐藏的&#xff09;
    • -w 统计词&#xff0c;它是以空格、空白字符进行区分的
      • 以分号分割的也会认为是一个字符&#xff0c;比如111&#xff0c;qqq这个就会认为是一个字符
  • cat -A 显示文件中所有的字符&#xff08;包括隐藏的&#xff09;

[root&#64;hf-01 ~]# wc -l 1.txt //显示文件的行数
22 1.txt
[root&#64;hf-01 ~]# wc -m 1.txt //显示文件中的字符数
468 1.txt
[root&#64;hf-01 ~]# vim 2.txt //在文件中编写两行&#xff0c;6个字符
[root&#64;hf-01 ~]# wc -m 2.txt //在查看的时候&#xff0c;会显示出8个字符
8 2.txt
[root&#64;hf-01 ~]# cat -A 2.txt //会统计所有的字符&#xff0c;包括隐藏字符
123$
avd$
[root&#64;hf-01 ~]# wc -w 2.txt //它是以空格、空白字符进行区分的
2 2.txt

uniq命令

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

uniq命令去重条件&#xff1a;需要先排序&#xff0c;再去重

[root&#64;hf-01 ~]# vim 2.txt
[root&#64;hf-01 ~]# cat 2.txt
123
avd 112,21a
123
avd
1
2
1
[root&#64;hf-01 ~]# uniq 2.txt
123
avd 112,21a
123
avd
1
2
1
[root&#64;hf-01 ~]# vim 2.txt
[root&#64;hf-01 ~]# cat 2.txt //查看文件内容
123
avd 112,21a
123
avd
1
1
2
[root&#64;hf-01 ~]# uniq 2.txt //会看到在更改排序后&#xff0c;去重了
123
avd 112,21a
123
avd
1
2
[root&#64;hf-01 ~]#

  • uniq命令和sort命令结合使用
    • 先排序&#xff0c;再去重

[root&#64;hf-01 ~]# sort 2.txt
1
1
123
123
2
avd
avd 112,21a
[root&#64;hf-01 ~]# sort 2.txt |uniq
1
123
2
avd
avd 112,21a
[root&#64;hf-01 ~]#

uniq命令参数 -c

  • uniq -c 统计重复次数

[root&#64;hf-01 ~]# sort 2.txt |uniq -c2 12 1231 21 avd1 avd 112,21a
[root&#64;hf-01 ~]#
8.12 tee_tr_split命令

tee命令

  • 清空文件内容 >1.txt

[root&#64;hf-01 ~]# > 2.txt
[root&#64;hf-01 ~]# cat 2.txt
[root&#64;hf-01 ~]#

  • tee命令和输出重定向>类似&#xff0c;重定向的同时还在屏幕显示
    • tee命令&#xff0c;就是重定向&#xff0c;把前面命令输出的结果打印到屏幕上
    • -a参数&#xff0c;追加重定向

[root&#64;hf-01 ~]# cat 3.txt
asda
123 fgdg,45
1
abc
cda
abc
1
[root&#64;hf-01 ~]# sort 3.txt |uniq -c |tee a.txt2 11 123 fgdg,452 abc1 asda1 cda
[root&#64;hf-01 ~]# cat a.txt2 11 123 fgdg,452 abc1 asda1 cda
[root&#64;hf-01 ~]#

tee命令参数-a

  • tee -a 就是追加重定向

[root&#64;hf-01 ~]# sort 3.txt |uniq -c |tee -a a.txt2 11 123 fgdg,452 abc1 asda1 cda
[root&#64;hf-01 ~]# cat a.txt2 11 123 fgdg,452 abc1 asda1 cda2 11 123 fgdg,452 abc1 asda1 cda
[root&#64;hf-01 ~]#

tr命令

  • tr 命令&#xff0c;用来替换字符的命令&#xff0c;tr &#39;a&#39; &#39;b&#39;,大小写替换tr &#39;[a-z]&#39; &#39;[A-Z]&#39;
    • 支持写多个字符替换

[root&#64;hf-01 ~]# echo "hanfeng" |tr &#39;[hf]&#39; &#39;[HF]&#39;
HanFeng
[root&#64;hf-01 ~]# echo "hanfeng" |tr &#39;h&#39; &#39;H&#39;
Hanfeng
[root&#64;hf-01 ~]# echo "hanfeng" |tr &#39;[a-z]&#39; &#39;[A-Z]&#39;
HANFENG
[root&#64;hf-01 ~]#

  • 字符替换数字的时候&#xff0c;需要注意格式
    • 替换数字的时候&#xff0c;需要去除方括号[]

[root&#64;hf-01 ~]# echo "hanfeng" |tr &#39;[a-z]&#39; &#39;[1]&#39; //错误示范&#xff0c;这样写会出错
]1]]]]]
[root&#64;hf-01 ~]# echo "hanfeng" |tr &#39;[a-z]&#39; &#39;1&#39; //在替换成数字的时候&#xff0c;需要去除方括号
1111111
[root&#64;hf-01 ~]#

split命令

  • split 切割&#xff0c;将一个大文件切割成很多个小文件
    • -b大小&#xff08;默认单位字节&#xff09;
      • 格式&#xff1a;split -b 100M bigfile
        • 若不写单位&#xff0c;会默认是字节
    • -l行数
      • 格式&#xff1a;split -l 1000 bigfile

[root&#64;hf-01 ~]# find /etc/ -type f -name "*conf" -exec cat {} >>a.txt \; //将etc目录下所有文件以conf结尾的文件全都输出重定向到a.txt文件中
[root&#64;hf-01 ~]# du -sh a.txt
252K a.txt
[root&#64;hf-01 ~]# mv a.txt 111/ //把a.txt文件移动到111目录下
[root&#64;hf-01 ~]# cd 111/ //切换到111目录下
[root&#64;hf-01 111]# ls
a.txt
[root&#64;hf-01 111]# split -b 1000 a.txt //单位是字节&#xff08;1000byte&#61;1k&#xff09;
[root&#64;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
等等等&#xff0c;只截取了一小部分
[root&#64;hf-01 111]# du -sh //查看目录下文件大小
2.3M .
[root&#64;hf-01 111]# du -sh * //会看到分割出来的都占据了一个块
252K a.txt
4.0K xaa
4.0K xab
4.0K xac
4.0K xad
等等等&#xff0c;只截取了一小部分
[root&#64;hf-01 111]# rm -f x*

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

  • split -b指定单位大小切割

[root&#64;hf-01 111]#
[root&#64;hf-01 111]# split -b 100k a.txt
[root&#64;hf-01 111]# ls
a.txt xaa xab xac
[root&#64;hf-01 111]# du -sh *
252K a.txt
100K xaa
100K xab
52K xac
[root&#64;hf-01 111]# rm -f x*
[root&#64;hf-01 111]#

  • 指定文件大小的同时&#xff0c;指定文件的名称

[root&#64;hf-01 111]# split -b 100k a.txt abc
[root&#64;hf-01 111]# ls
abcaa abcab abcac a.txt
[root&#64;hf-01 111]# split -b 100k a.txt abc.
[root&#64;hf-01 111]# ls
abcaa abc.aa abcab abc.ab abcac abc.ac a.txt
[root&#64;hf-01 111]# rm -f abc*

切割的文件默认是以x开头&#xff01;&#xff01;&#xff01;

split命令的参数-l

  • split -l 指定行数

[root&#64;hf-01 111]# split -l 1000 a.txt
[root&#64;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&#64;hf-01 111]# wc -l *6548 a.txt1000 xaa1000 xab1000 xac1000 xad1000 xae1000 xaf548 xag13096 总用量
[root&#64;hf-01 111]#
8.13 shell特殊符号下

特殊符号

$ 变量前缀&#xff0c;!$组合&#xff0c;正则里面表示行尾
&#xff1b;多条命令写到一行&#xff0c;用分号分割
~ 用户家目录&#xff0c;后面正则表达式表示匹配符
& 放到命令后面&#xff0c;会把命令丢到后台
> 正确重定向
>> 追加重定向
2> 错误重定向
2>> 错误追加重定向
&> 正确和错误输出重定向
[ ] 指定字符中的一个&#xff0c;[0-9],[a-zA-Z],[abc]
|| 和 && &#xff0c;用于命令之间

  • || 表示 或者 的意思
    • 两条命令之间&#xff0c;第一条命令如果执行不成功&#xff0c;那就会执行第二条
      • 若第一条命令执行成功&#xff0c;那么就不会再执行第二条命令
  • && 表示 和 的意思
    • 两条命令之间&#xff0c;第一条命令执行成功后&#xff0c;才会执行第二条命令

判断一个目录是否存在

  • [ -d haha ]判断这个是否是一个目录&#xff0c;是否存在的
    • 如果是目录&#xff0c;并且存在&#xff0c;那 [ -d haha ] 执行成功

[root&#64;hf-01 ~]# [ -d haha ] || mkdir haha
[root&#64;hf-01 ~]# ls
111 1_heard.txt 1.txt 2.txt 3.txt haha
[root&#64;hf-01 ~]# [ -d haha ] && mkdir haha
mkdir: 无法创建目录"haha": 文件已存在
[root&#64;hf-01 ~]#
相关测验题目 扩展

  1. source exec 区别

  2. Linux特殊符号大全

  3. sort并未按ASCII排序


转:https://my.oschina.net/u/3707314/blog/1574943



推荐阅读
  • 【Python 爬虫】破解按照顺序点击验证码(非自动化浏览器)
    #请求到验证码base64编码json_img_datajson_raw.get(Vimage)#获取到验证码编码 #保存验证码图片到本地defbase64_to_img(bstr ... [详细]
  • 提升Python编程效率的十点建议
    本文介绍了提升Python编程效率的十点建议,包括不使用分号、选择合适的代码编辑器、遵循Python代码规范等。这些建议可以帮助开发者节省时间,提高编程效率。同时,还提供了相关参考链接供读者深入学习。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Python15行代码实现免费发送手机短信,推送消息「建议收藏」
    Python15行代码实现免费发 ... [详细]
  • 对于一般的扩展包,我们一般直接pipinstallxxx即可安装,但是unrar直接安装后,发现并不能通过Python程序实现解压的功能& ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • Python中sys模块的功能及用法详解
    本文详细介绍了Python中sys模块的功能及用法,包括对解释器参数和功能的访问、命令行参数列表、字节顺序指示符、编译模块名称等。同时还介绍了sys模块中的新功能和call_tracing函数的用法。推荐学习《Python教程》以深入了解。 ... [详细]
  • python oj刷题网站_中小学计算机竞赛去哪里刷题?橙旭园OJ系统上线啦!
    原标题:中小学计算机竞赛去哪里刷题?橙旭园OJ系统上线啦!相信许多人在学习完编程之后都会茫然,感觉自己无所不能࿰ ... [详细]
  • Linux一键安装web环境全攻略
    摘自阿里云服务器官网,此处一键安装包下载:点此下载安装须知1、此安装包可在阿里云所有Linux系统上部署安装,此安装包包含的软件及版本为& ... [详细]
  • 1、PLSQLDeveloper记住登陆密码在使用PLSQLDeveloper时,为了工作方便希望PLSQLDeveloper记住登录Oracle的用户名和密码&#x ... [详细]
  • 网络安全是一个非常重要的课题,基本上你运行的服务后台越多,你就可能打开更多的安全漏洞.如果配置的恰当的话,Linux本身是非常安全可靠的,假使在Linux系统中有某个安全缺陷,由于Linu ... [详细]
  • 转载自: http:hi.baidu.comzvjqblogitem9cf74a9767f8d55fd0135ef2.html添加扩展的基本步骤:1、进入php源代码目录:#cdu ... [详细]
  • linux下的mesa一般版本比较低,按照高版本mesa1.下载代码下载路径:https:www.mesa3d.org用git下载容易失败。用Downl ... [详细]
  • IP双栈环境下网络应用迁移
    IPv4向IPv6迁移有多种途径,在选择具体的迁移方式时,当前环境中运行的应用是否支持IPv6是重要的考量因素之一,同时在编写新的应用时,需要考虑新编写的应用不仅可以适应当前主流的IPv4环境, ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有