Shell 特殊符_cut 命令
特殊符号:
* 任意个字符
? 任意一个字符
- # 注释字符 (在配置文件的命令前加 #则命令不生效; shell 脚本某一行加 #则这一行不被执行, 通常是注释)
- [root@localhost ~]# #ls -a
- [root@localhost ~]#
\ 脱义字符
- [root@localhost ~]# c='$a$b'
- [root@localhost ~]# echo $c
- $a$b
- [root@localhost ~]# c=\$a\$b
- [root@localhost ~]# echo $c
- $a$b
| 管道符
管道相关的命令:
Cut 截取字段
- [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1 以: 为节点截取一块字符
- root
- bin
- [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 以: 为节点截取两块字符
- root:x
- bin:x
- [root@localhost ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
- root:x:0
- bin:x:1
- [root@localhost ~]# cat /etc/passwd |head -2 |cut -c 4 截取一段的第 4 个字符
- Sort
- [root@localhost ~]# sort /etc/passwd
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- bin:x:1:1:bin:/bin:/sbin/nologin
- chrony:x:998:996::/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
- hyc:x:1000:1000::/home/hyc:/bin/bash
Sort 默认按 ASCII 码排序
- [root@localhost ~]# cat /etc/passwd > 1.txt 文件中包含英文数字特殊符号等
- [root@localhost ~]# sort 1.txt 按 ASCII 码排序 (特殊符号在最前 (* 除外), 然后数字从小到大排序, 再然后是字母等)
- <>
- ><'
- #$^&
- 213444
- 234532
- 234aaa
- 23aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- 34aaaaaaaaaaaaaaaaaaaaaaaaaaaadd
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- bin:x:1:1:bin:/bin:/sbin/nologin
- chrony:x:998:996::/var/lib/chrony:/sbin/nologin
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- dbus:x:81:81:System message bus:/:/sbin/nologin
- *$fas
- ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
- games:x:12:100:games:/usr/games:/sbin/nologin
- [root@localhost ~]# sort -n 1.txt
- <>
- ><'
- #$^&
- adm:x:3:4:adm:/var/adm:/sbin/nologin
- bin:x:1:1:bin:/bin:/sbin/nologin
- chrony:x:998:996::/var/lib/chrony:/sbin/nologin
- daemon:x:2:2:daemon:/sbin:/sbin/nologin
- dbus:x:81:81:System message bus:/:/sbin/nologin
- *$fas
- 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
- hyc:x:1000:1000::/home/hyc:/bin/bash
- 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
- polkitd:x:999:997:User for polkitd:/:/sbin/nologin
- postfix:x:89:89::/var/spool/postfix:/sbin/nologin
- root:x:0:0:root:/root:/bin/bash
- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
- sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- sync:x:5:0:sync:/sbin:/bin/sync
- systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
- 23aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- 34aaaaaaaaaaaaaaaaaaaaaaaaaaaadd
- 234aaa
- 213444
- 234532
*-n 会按数字排序, 其他字母特殊符号会被当 0 处理
- [root@localhost ~]# sort -nr 1.txt
- 234532
- 213444
- 234aaa
- 34aaaaaaaaaaaaaaaaaaaaaaaaaaaadd
- 23aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
- systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
- sync:x:5:0:sync:/sbin:/bin/sync
* 反向排序, 大的在前, 小的在后
Sort -t 分隔符
- [root@localhost ~]# cat -A 2.txt 显示包括隐藏内容在内的所有内容
- 1234$
- 4567$
- [root@localhost ~]# wc -l 2.txt 统计行数
- 2 2.txt
- [root@localhost ~]# wc -m 2.txt 统计字符数
- 10 2.txt
- [root@localhost ~]# wc -w 2.txt 统计词数 (每个词用空格或空白符号隔开)
- 2 2.txt
- [root@localhost ~]#
去重复字符
- [root@localhost ~]# cat 2.txt
- 1234
- 1234
- qwuh
- chrf
- ydry
- ydry
- [root@localhost ~]# uniq 2.txt
- 1234
- qwuh
- chrf
- ydry
- [root@localhost ~]# sort 2.txt |uniq 先排序, 再去重复
- 1234
- chrf
- qwuh
- rrrr
- ydry
- [root@localhost ~]# sort 2.txt 去重复
- 1234
- 1234
- 1234
- chrf
- qwuh
- rrrr
- ydry
- ydry
* 某两行字符相同但被隔了几行则这两行不会被去重复, 先排序再去重则不会出现重复
- [root@localhost ~]# sort 2.txt
- 1234
- 1234
- 1234
- chrf
- qwuh
- rrrr
- ydry
- ydry
- [root@localhost ~]# sort 2.txt |uniq -c
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
* 先排序再统计重复次数
- tee_tr_split
- [root@localhost ~]# sort 2.txt|uniq -c > a.txt
- [root@localhost ~]# cat a.txt
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
- [root@localhost ~]# sort 2.txt|uniq -c|tee a.txt
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
tee 与 > 效果相同, 但 tee 会把结果打印在屏幕上,> 不会
- [root@localhost ~]# sort 2.txt|uniq -c|tee -a a.txt a 参数表示追加 (>>)
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
- [root@localhost ~]# cat a.txt
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
- 3 1234
- 1 chrf
- 1 qwuh
- 1 rrrr
- 2 ydry
tr 命令
- [root@localhost ~]# echo "aminglinux"|tr '[al]' '[AL]' 替换字符
- AmingLinux
- [root@localhost ~]# echo "aminglinux"|tr '[a]' '[A]'
- Aminglinux
- [root@localhost ~]# echo "aminglinux"|tr '[a-z]' '[A-Z]' 将所有小写字母替换为大写
- AMINGLINUX
- [root@localhost ~]# echo "aminglinux"|tr '[a-z]' '[1]' a-z 替换为 1 错误用法
- 1]]]]]]]]]
- [root@localhost ~]# echo "aminglinux"|tr '[a-z]' '1' 正确用法
- 1111111111
Split 命令
- -b
- [root@localhost a]# split -b 1000 a.txt 按每个文件 1000 字节切割文件 (不带单位默认字节)
- [root@localhost a]# ls
- a.txt xan xbb xbp xcd xcr xdf xdt xeh xev xfj xfx xgl xgz xhn xib
- xaa xao xbc xbq xce xcs xdg xdu xei xew xfk xfy xgm xha xho xic
- xab xap xbd xbr xcf xct xdh xdv xej xex xfl xfz xgn xhb xhp xid
- [root@localhost a]# rm -f x* 删除目录下所有切割后的文件
- [root@localhost a]# ls
- a.txt
- [root@localhost a]# split -b 100k a.txt 指定大小 100k
- [root@localhost a]# ls
- a. txt xaa xab xac
- b. [root@localhost a]# du -sh *
- c. 212K a.txt
- d. 100K xaa
- e. 100K xab
- f. 12K xac
- [root@localhost a]# split -b 100k a.txt abc 指定切割后文件的前缀
- [root@localhost a]# ls
- abcaa abcab abcac a.txt xaa xab xac
- -l
- [root@localhost a]# wc -l a.txt 按行切割文件
- 5395 a.txt
- [root@localhost a]# split -l 1000 a.txt
- [root@localhost a]# wc -l *
- 5395 a.txt
- 1000 xaa
- 1000 xab
- 1000 xac
- 1000 xad
- 1000 xae
- 395 xaf
10790 总用量
Shell 特殊符号
$ 变量前缀 !$ 一个组合, 正则表达式中表示行尾
; 当要将多条命令写到一行时用于分割命令
[root@localhost a]# ls 1.txt;wc -l a.txt
ls: 无法访问 1.txt: 没有那个文件或目录
5395 a.txt
~ 用户家目录, 后面正则表达式表示匹配符
& 把命令放到后面, 会将命令放到后台
&> 对正确和错误的内容使用相同的重定向
>,>>,2>,2>>
[] 指定字符中的一个 ([0-9][a-zA-Z])
- ||
- [root@localhost a]# wc -l a.txt || ls 1.txt 第一条命令执行不成功则执行第二条, 否则不再向下执行
- 5395 a.txt
- [root@localhost a]# ls 1.txt || wc -l a.txt
ls: 无法访问 1.txt: 没有那个文件或目录
- 5395 a.txt
- [root@localhost a]# wc -l a.txt && ls -l a.txt 只有前面命令执行成功才会执行后续命令
- 5395 a.txt
-rw-r--r--. 1 root root 215603 3 月 11 23:37 a.txt
[root@localhost a]# wc -l 1.txt && ls -l a.txt
wc: 1.txt: 没有那个文件或目录
当用户要创建一个目录, 若目录不存在才创建, 存在则不创建:
- [root@localhost a]# [ -d aminglinux ] || mkdir aminglinux || 之前的命令用于判断目录是否存在, 存在则不继续后面的命令, 否则执行后续的命令
- [root@localhost a]# ls
- aminglinux a.txt xaa xab xac xad xae xaf
- [root@localhost a]# [ -d aminglinux ] && mkdir aminglinux 判断目录是否存在, 然后强制创建目录
mkdir: 无法创建目录 "aminglinux": 文件已存在 执行后面的命令后, 报错产生
来源: http://www.bubuko.com/infodetail-2524549.html