您的位置:首页 > 运维架构 > Linux

Linux学习(二十二)Shell基础(三)特殊符号、sort、wc、uniq、tee、tr、split

2020-02-03 21:54 1376 查看

一、特殊符号

* :任意个任意字符

[root@ruanwenwu02 ~]# ls 1*
1bak.zip  1.tar  1.zip.bz2

1:
1.txt  2
[root@ruanwenwu02 ~]# ls 1*2

?:一个任意字符

[root@ruanwenwu02 ~]# ls 2?tar
2.tar

#:注释

[root@ruanwenwu02 ~]# #ls
[root@ruanwenwu02 ~]#

\:脱意

[root@ruanwenwu02 ~]# a=3
[root@ruanwenwu02 ~]# echo $a
3
[root@ruanwenwu02 ~]# echo \$a
$a
[root@ruanwenwu02 ~]# echo "\$a"
$a

|:管道符号

管道符号的作用,是把上一条命令的输出作为下一条命令的标准输入:

[root@ruanwenwu02 ~]# ls|wc -l
21

二、sort

sort命令是用来排序的。

默认按照首字母的ascii码排序:

[root@ruanwenwu02 ~]# ls|sort
1
1bak.zip
1.tar
1.zip.bz2
2
2.tar
3
33.tar
3.tar.gz
3.zip
4.tar
4.tar.bip2
4.zip
5
5.zip
6
7.txt
8.txt
999.txt
9.txt
[root@ruanwenwu02 ~]# cat 3.txt | sort

[
$
$
%^
3
33
55
6
6
666
dfd
dfdf
e
gr
kQ$
Q$
Q$
t

按数字排序:

[root@ruanwenwu02 ~]# sort -n 3.txt

[
$
$
%^
dfd
dfdf
e
gr
kQ$
Q$
Q$
t
3
6
6
33
55
666

实验发现当按数字排序的时候,字母和特殊字符都被当成0了。

倒序:

[root@ruanwenwu02 ~]# sort -r 3.txt
t
Q$
Q$
kQ$
gr
e
dfdf
dfd
666
6
6
55
33
3
%^
$
$
[

分段排序:

[root@ruanwenwu02 ~]# sort -t: -nrk3 666.txt
ruanwenwu:x:1000:1000::/home/ruanwenwu:/bin/bash
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

三、cut

cut命令是用来切割的。-d指定分隔符,-f指定段数,-c指定几个字符

[root@ruanwenwu02 ~]# cut -d : -f 3,4 666.txt
0:0
1:1
2:2
3:4
4:7
5:0
6:0
7:0
8:12
11:0
12:100
14:50
99:99
999:997
192:192
81:81
59:996
59:59
89:89
74:74
1000:1000
[root@ruanwenwu02 ~]# cut -d : -f 3,4 -c1 666.txt
cut: 只能指定列表中的一种类型
Try 'cut --help' for more information.

实验说明,不能既分段切割,又截取字符。

截取字符:

[root@ruanwenwu02 ~]# cut -c 2-4 666.txt
oot
in:
aem
dm:
p:x
ync

四、wc

wc命令用来统计行数(最常用),单词数和字数。

wc -l统计行数。

wc -w统计单词数。

wc -m或者wc -c统计字母数。这两个命令会把隐藏的字母算在内。

[root@ruanwenwu02 ~]# vim 777.txt
[root@ruanwenwu02 ~]# wc -c 777.txt
19 777.txt
[root@ruanwenwu02 ~]# cat 777.txt
df dd
cc
dd dd dd

[root@ruanwenwu02 ~]# cat -A 777.txt
df dd$
cc$
dd dd dd$
$
[root@ruanwenwu02 ~]# wc -m 777.txt
19 777.txt
[root@ruanwenwu02 ~]# wc -w 777.txt
6 777.txt

五、uniq

uniq命令经常和sort命令一起用。因为如果两个相同的行不在一起,就无法uniq。

[root@ruanwenwu02 ~]# uniq 1.txt
3
4
3
5
6
7
87
8

我们先sort再uniq看看呢:

[root@ruanwenwu02 ~]# sort 1.txt|uniq
3
4
5
6
7
8
87

uniq -c显示每行重复的次数:

[root@ruanwenwu02 ~]# sort 1.txt|uniq -c
2 3
1 4
1 5
1 6
1 7
1 8
1 87

六、tee

tee命令的作用是接受标准输入,并重定向,将标准输入打印出来。

[root@ruanwenwu02 ~]# cat 1.txt|tee 3.txt
dffdf
[root@ruanwenwu02 ~]# cat 1.txt|tee -a 3.txt
dffdf
[root@ruanwenwu02 ~]# cat 1.txt|tee -a 3.txt
dffdf
[root@ruanwenwu02 ~]# cat 1.txt|tee -a 3.txt
dffdf
[root@ruanwenwu02 ~]# cat 3.txt
dffdf
dffdf
dffdf
dffdf

七、tr

tr命令的作用是将标准输入替换。

[root@ruanwenwu02 ~]# tr 'a-z' 'A-Z' 'sdfsdfsgdgfs'
tr: 额外的操作数 "sdfsdfsgdgfs"
Try 'tr --help' for more information.
[root@ruanwenwu02 ~]# tr 'a-z' 'A-Z'
dfdfdfdfdfdfd
DFDFDFDFDFDFD

八、split

split是切割文件。split可以按行切(split -l),也可以按大小切(split -b)。

首先我们来准备一个大文件。将系统中所有的conf文件合并到a.txt。

[root@ruanwenwu02 ~]# find / -type f -name "*.conf" -exec cat {} >> a.txt \;

 按行切:

[root@ruanwenwu02 ~]# du -sh a.txt
544K    a.txt
[root@ruanwenwu02 ~]# wc -l a.txt
21746 a.txt
[root@ruanwenwu02 ~]# mkdir /tmp/test2
[root@ruanwenwu02 ~]# mv a.txt /tmp/test2/
[root@ruanwenwu02 ~]# cd !$
cd /tmp/test2/
[root@ruanwenwu02 test2]# ls
a.txt
[root@ruanwenwu02 test2]# ls -l
总用量 544
-rw-r--r--. 1 root root 554540 11月 18 12:37 a.txt
[root@ruanwenwu02 test2]# wc -l a.txt
21746 a.txt
[root@ruanwenwu02 test2]# wc -l 10000 a.txt
wc: 10000: 没有那个文件或目录
21746 a.txt
21746 总用量
[root@ruanwenwu02 test2]# ls
a.txt
[root@ruanwenwu02 test2]# split -l 10000 a.txt
[root@ruanwenwu02 test2]# ls
a.txt  xaa  xab  xac

 

按大小切:

[root@ruanwenwu02 test2]# !find
find / -type f -name "*.conf" -exec cat {} >> a.txt \;
[root@ruanwenwu02 test2]# ls
a.txt
[root@ruanwenwu02 test2]# split -b 100k a.txt
[root@ruanwenwu02 test2]# ls
a.txt  xaa  xab  xac  xad  xae  xaf

 指定分割文件前缀:

[root@ruanwenwu02 test2]# split -b 100k a.txt abc.
[root@ruanwenwu02 test2]# ls
abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  a.txt

 九、特殊符号

注意:||是如果前面的命令执行了,后面的就不执行了。

[root@ruanwenwu02 test2]# [ -d ruanwenwu ] || mkdir ruanwenwu
[root@ruanwenwu02 test2]# ls
abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  a.txt  ruanwenwu

 

转载于:https://www.cnblogs.com/doubilaile/p/7856396.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
alexlee0123 发布了0 篇原创文章 · 获赞 0 · 访问量 498 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐