1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 8.10-8.13 shell的cut sort_wc_uniq tee_tr_split 特殊符号

8.10-8.13 shell的cut sort_wc_uniq tee_tr_split 特殊符号

时间:2022-09-28 08:22:37

相关推荐

8.10-8.13 shell的cut sort_wc_uniq tee_tr_split 特殊符号

8.10shell特殊符号cut命令

8.11sort_wc_uniq命令

8.12tee_tr_split命令

8.13shell特殊符号下

8.10 shell特殊符_cut命令

特殊符号

*任意个任意字符

?任意一个字符

#注释字符

在配置文件首端加入# 代表注释,后面的参数无意义,不生效。只是起到解释说明作用

\脱义字符

[root@centos7 ~]# a=1

[root@centos7 ~]# b=2

[root@centos7 ~]# echo $c

12

[root@centos7 ~]# c='$a$b'

[root@centos7 ~]# echo $c

$a$b

[root@centos7 ~]# c=\$a\$b 在有效参数字符前面加个\把$的参数脱义,让其不生效,使其变成普通字符.

[root@centos7 ~]# echo $c

$a$b

|管道符

几个和管道符相关的命令

cut 分割,截取作用。 -d 分隔符 -f指定段号 -c 指定第几个字符

[root@centos7 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1

root

bin

查看/etc/passwd的头2行,用:分割,截取第一段。

[root@centos7 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3

root:x:0

bin:x:1

查看/etc/passwd的头2行,用:分割,截取第一到第三段。

8.11 sort_wc_uniq命令

cut -c 指定第几个字符(使用-c的话 就不要使用-d -f了)

[root@centos7 ~]# cat /etc/passwd |head -2

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@centos7 ~]# cat /etc/passwd |head -2 |cut -c 4

t

:

sort排序

什么时候用#sort ?

例如现在拿到一列数字或者字符串,让它们按不同要求进行排序。

sort经常结合uniq一起使用。

test

[root@centos7 ~]# sort /etc/passwd

可以看出sort后的文件名从小到大顺序排序。(sort根据ASCII码来进行排序)

test2

[root@centos7 ~]# head /etc/passwd >> 1.txt

[root@centos7 ~]# vi 1.txt

[root@centos7 ~]# sort 1.txt

<

>

{

1.txt

2222211111

2222222aaaaa

2.txt

444448888sss

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

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

*wwwweeq

此排序是根据ASCII码来排序得来。

-n 数字排序

使用此模式后,除数字都会被无视,被认为是0,而0是比1再前,所以第一时间显示会是非数字的内容。

test3

[root@centos7 ~]# 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

*wwwweeq

1.txt

2.txt

2222222aaaaa

444448888sss

2222211111

-r 反序

与-n相反效果。

-t 分隔符 -kn1/-kn1,n2

用得不错 简单了解即可

wc -l 统计行数 -m 统计字符数(字数) -w 统计词

test4

[root@centos7 ~]# vi 2.txt敲入6个字符

123

abc

[root@centos7 ~]# wc -m 2.txt 为什么统计字符会是8个字符,原因是因为有了2个$,换行会产生$,只是一般会被隐藏。

8 2.txt

[root@centos7 ~]# cat -A 2.txt cat -A可以查看隐藏字符。

123$

abc$

[root@centos7 ~]# wc -w 2.txt

2 2.txt

统计有2个词。以空格或空白字符(, . $ < > )相隔一个整体未一个词。

uniq 去重(重复),-c统计行数

test5

[root@centos7 ~]# vi 2.txt

123

abc 111,222

123

abc

1

1

2

使用uniq之前,一定要排序后,再uniq去重。所以搭配sort就最佳效果了。

test6

[root@centos7 ~]# sort 2.txt

1

1

123

123

2

abc

abc 111,222

[root@centos7 ~]# sort 2.txt |uniq

1

123

2

abc

abc 111,222

统计重复出现次数 -c

[root@centos7 ~]# sort 2.txt |uniq -c

2 1

2 123

1 2

1 abc

1 abc 111,222

8.12 tee_tr_split命令

tee和 >类似,重定向的同时还在屏幕显示。

test1

[root@centos7 ~]# sort 2.txt |uniq -c > a.txt

[root@centos7 ~]# cat a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

[root@centos7 ~]# sort 2.txt |uniq -c |tee a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

由此可以见 # sort 2.txt |uniq -c |tee a.txt的效果,相当于# sort 2.txt |uniq -c > a.txt加# cat a.txt的效果。tee的作用是追加并显示追加内容。

test2

重复测试多一次

[root@centos7 ~]# >a.txt 前空白>接文件,表示清空文件内容。

[root@centos7 ~]# cat a.txt

[root@centos7 ~]# sort 2.txt |uniq -c |tee a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

[root@centos7 ~]# cat a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

效果一样 没有变化。

tee -a 表示再次追加

test3

[root@centos7 ~]# sort 2.txt |uniq -c |tee -a a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

[root@centos7 ~]# cat a.txt

2 1

2 123

1 2

1 abc

1 abc 111,222

2 1

2 123

1 2

1 abc

1 abc 111,222

2 1

2 123

1 2

1 abc

1 abc 111,222

tr 替换字符, tr 'a' 'b',大小写替换tr'[a-z]''[A-Z]'

test4

[root@centos7 ~]# echo "aminglinux" |tr '[al]' '[AL]'

AmingLinux

把aminglinux的aming中的a改成A,linux的l改成L。

test5

[root@centos7 ~]# echo "aminglinux" |tr 'a' 'A'

Aminglinux

a变成A

test6

[root@centos7 ~]# echo "aminglinux" |tr '[a-z]' '[A-Z]'

AMINGLINUX

全部变成大写。

test7

[root@centos7 ~]# echo "aminglinux" |tr '[a-z]' '1'

1111111111

把aminglinux变成1

记住tr支持多个替换,而且它们是一一对应的。

split 切割 -b大小(默认单位字节),-l行数

大文件切割成小文件。

[root@centos7 ~]# split -b 100M bigfile

针对文件,切割100M。

[root@centos7 ~]# split -l 1000 bigfire

针对行数,切割1000行。

test 8

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

[root@centos7 ~]# du -sh a.txt

256Ka.txt

[root@centos7 ~]# mv a.txt test/

[root@centos7 ~]# cd test/

[root@centos7 test]# ls

a.txt

[root@centos7 test]# split -b 1000 a.txt

不加容量单位,默认是字节

指定大小分割

[root@centos7 test]# split -b 100k a.txt

[root@centos7 test]# ls

a.txt xaa xab xac

[root@centos7 test]# du -sh *

240Ka.txt

100Kxaa

100Kxab

40Kxac

删除x*开头的文件

[root@centos7 test]# rm -rf x*

[root@centos7 test]# ls

a.txt

指定分割后的名称

[root@centos7 test]# split -b 100k a.txt FG

[root@centos7 test]# ls

a.txt FGaa FGab FGac

[root@centos7 test]# du -sh *

240Ka.txt

100KFGaa

100KFGab

40KFGac

分割行数

分割1000行

[root@centos7 test]# split -l 1000 a.txt

[root@centos7 test]# ls

a.txt FGaa FGab FGac xaa xab xac xad xae xaf xag

[root@centos7 test]# wc -l *

6112 a.txt

2401 FGaa

2695 FGab

1016 FGac

1000 xaa

1000 xab

1000 xac

1000 xad

1000 xae

1000 xaf

112 xag

8.13 shell特殊符号(下)

$变量前缀,!$组合,正则里面行尾

;多条命令写到一行,用分号;分割

[root@centos7 ~]# ls 1.txt ; wc -l 2.txt

1.txt

7 2.txt

~用户给家目录,后面正则表达式表达匹配符

&放到命令后面,会把命令丢到后台.

> >> 2> 2>> &>

> 正确重定向,会把之前的文件覆盖掉。

>> 追加重定向,正确输出

2> 错误重定向,错误输出

2>> 错误追加重定向,错误输出

&>正确与错误输出重定向。

[]指定字符中的一个,[0-9],[a-z][A-Z],[abc]

||(两个管道符)和&&,用于命令之间。(判断作用,||表示或,&&表示才。)

||用法,

用于两命令之间,如果执行两条命令,其中前面是错误命令的话,和执行第二条命令。如果前面命令是正确的话,后面的命令将不会执行。

[root@centos7 ~]# ls aaaa.txt || wc -l 2.txt

ls: 无法访问aaaa.txt: 没有那个文件或目录

7 2.txt

[root@centos7 ~]# wc -l 2.txt || ls 1.txt

7 2.txt

&&用法,

两条命令,如果前面的命令正确,后面的命令才会被执行。如果前面的命令错误,后面的命令将不会被执行。

[root@centos7 ~]# ls 1.txt && wc -l 2.txt

1.txt

7 2.txt

前面命令成功了,后面的命令继续被执行。

[root@centos7 ~]# ls 1a.txt && wc -l 2.txt

ls: 无法访问1a.txt: 没有那个文件或目录

前面命令失败了,后面的命令没有被执行。

test

创建一个目录aminglinux/,条件是不存在才创建,如果存在就不创建。

[root@centos7 ~]# [ -d aminglinux ] || mkdir aminglinux

[root@centos7 ~]# ls

1.txt 2.txt 4.txt AA.txt anaconda-ks.cfg A.txt err test

1.xtx 3.txt a_(2).txtaminglinuxanaconda-ks.cfg.1 bb.txt temp.1

解释 [ -d aminglinux ] -d是否一个目录,目录是否存在。

如果文件存在,才去创建,此时会出现报错。

[root@centos7 ~]# [ -d aminglinux ] && mkdir aminglinux

mkdir: 无法创建目录"aminglinux": 文件已存在

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。