码迷,mamicode.com
首页 > 其他好文 > 详细

2018-1-12 5周5次课

时间:2018-01-12 21:23:13      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:[1]   特殊   执行   匹配   创建   translate   man   文件的   符号   

8.10 shell特殊符_cut命令


*              任意个任意字符

?              任意一个字符     ?.txt

#              注释字符 不生效,说明文字

\              脱义字符 用在符号前

|              管道符


cut分割

    -d     分隔符

    -f     指定段号

    -c     指定第几个字符

[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
t
:
[root@localhost ~]#cat /etc/passwd | head -2 |cut -c 4,5
t:
:x





8.11 sort_wc_uniq命令


·sort 排序

    -n 以数字排序

    -r 反序

    -t 指定栏位分隔符

    -k 是指定需要爱排序的栏位 -nk 1/-nk 1,2

[root@localhost ~]#sort 1.txt
<
>
]
{
1.txt
222222aaaaaaaaa
22333333
22aaa
231312131
2.txt
4888888888888888888adslkfj;a
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
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

默认排序是按照 空字符 - 特殊符号 - 数字 - 字母


[root@localhost ~]#sort -n 1.txt
<
>                                    ##字母和特殊符号被认为是0,被放在前面
]
{
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
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
1.txt
2.txt
22aaa
222222aaaaaaaaa
22333333
231312131
4888888888888888888adslkfj;a
[root@localhost ~]#sort -r 1.txt            ##倒序排列
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
*slkdf
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
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
4888888888888888888adslkfj;a
2.txt
231312131
22aaa
22333333
222222aaaaaaaaa
1.txt
{
]
>
<


·wc 统计

    -l 统计行数

    -m 统计字符数

    -w 统计词

[root@localhost ~]#wc -l 1.txt
21 1.txt
[root@localhost ~]#wc -m 2.txt
8 2.txt
[root@localhost ~]#cat -A 2.txt
123$
abc$
[root@localhost ~]#wc -w 2.txt
2 2.txt


·uniq 去重

    -c统计行数

[root@localhost ~]#cat 2.txt
123
abc 1111,2222
123
abc
1
1
2
[root@localhost ~]#uniq 2.txt
123
abc 1111,2222
123
abc
1
2
[root@localhost ~]#sort -n 2.txt | uniq
abc
abc 1111,2222
1
2
123
[root@localhost ~]#sort -n 2.txt | uniq -c
1 abc
1 abc 1111,2222
2 1
1 2
2 123


参考:http://man.linuxde.net/sort





8.12 tee_tr_split命令


· tee 和 > 类似,重定向的同时还在屏幕显示,而 > 不显示过程

[root@localhost ~]# sort 2.txt
1
1
123
123
2
abc
abc 1111,2222
[root@localhost ~]# sort 2.txt |uniq -c
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@localhost ~]# sort 2.txt |uniq -c > a.txt
[root@localhost ~]# sort 2.txt |uniq -c |tee b.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@localhost ~]# cat b.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222


·tee -a 和 >> 类似,追加的同时还在屏幕显示,而 >>不显示过程

[root@localhost ~]# sort 2.txt |uniq -c |tee -a b.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@localhost ~]# cat b.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
2 1
2 123
1 2
1 abc
1 abc 1111,2222


· tr 替换字符:tr 'a' 'b'           (tr=translate)

大小写替换 tr '[a-z]' '[A-Z]'
[root@localhost ~]# echo "arsenal" | tr '[al]' '[AL]'
ArsenAL
[root@localhost ~]# echo "arsenal" | tr 'a' 'A'
ArsenAl
[root@localhost ~]# echo "arsenal" | tr '[a-z]' '[A-Z]'
ARSENAL
[root@localhost ~]# echo "arsenal" | tr '[a-z]' '1'
1111111
[root@localhost ~]# echo "arsenal" | tr '[a-z]' '[1]'      ##单个数字或字母不要用[ ]括起来,会出错
1]]]]1]

 

·split 切割           (文件过大,切割方便操作)

    -b 按大小切割(默认单位字节,如果不写单位,就会按100B切割)

    -l  按行数切割

[root@localhost ~]# split -b 100M bigfile##把文件按每100M大小切割
[root@localhost ~]# split -l 1000 bigfile##把文件按每1000行进行切割
[root@localhost ~]# ls
1.txt  2.txt  anaconda-ks.cfg  b.txt
[root@localhost ~]# find /etc/ -type f -name "*.conf" -exec cat {} >> a.txt \;
[root@localhost ~]# ll -h a.txt
-rw-r--r--. 1 root root 238K 1月   8 13:21 a.txt
[root@localhost ~]# mkdir test/
[root@localhost ~]# mv a.txt test/
[root@localhost ~]# cd test/
[root@localhost test]# ls
a.txt
[root@localhost test]# split -b 1000 a.txt
文件过多,不详细列出
[root@localhost test]# rm -f x*
[root@localhost test]# ll -h
总用量 240K
-rw-r--r--. 1 root root 238K 1月   8 13:21 a.txt


·指定切割出文件的前缀 (切割后前缀为abc后面根据aa,ab...ba,bb,...排列)

[root@localhost test]# split -b 100k a.txt abc
[root@localhost test]# ls
abcaa  abcab  abcac  a.txt
[root@localhost test]# rm -f abc*
[root@localhost test]# ls
a.txt
[root@localhost test]# split -b 100k a.txt abc.
[root@localhost test]# ls
abc.aa  abc.ab  abc.ac  a.txt


·按行来切割:split -l 行数 文件

[root@localhost test]# split -l 1000 a.txt
[root@localhost test]# wc -l *
2391 abc.aa
2694 abc.ab
1011 abc.ac
6096 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
96 xag





8.13 shell特殊符号(下)

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

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

[root@localhost test]# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
[root@localhost test]# for i in `seq 1 10`; do echo $i; done
[root@localhost test]# cd
[root@localhost ~]# ls
1.txt  2.txt  anaconda-ks.cfg  b.txt  test
[root@localhost ~]# ls 1.txt ; wc -l 2.txt       ##想要同时执行多条命令,用 ; 分隔
1.txt
7 2.tx

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

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

>                 正确重定向,覆盖源文件

>>              追加正确重定向

2>               错误重定向

2>>             追加错误重定向

&>               正确错误全部重定向到一个文件

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

|| 和 &&       用于命令之间

[root@localhost ~]# ls
1.txt  2.txt  anaconda-ks.cfg  b.txt  test
[root@localhost ~]# ls a.txt || wc -l 2.txt        ##||(第一条命令不成功,则执行第二条)
ls: 无法访问a.txt: 没有那个文件或目录
7 2.txt
[root@localhost ~]# ls 1.txt || wc -l 2.txt         ##||(第一条命令成功,则不执行第二条)
1.txt
[root@localhost ~]# ls a.txt && wc -l 2.txt         ##&&(第一条命令不成功,则不执行后面命令)
ls: 无法访问a.txt: 没有那个文件或目录
[root@localhost ~]# ls 1.txt && wc -l 2.txt         ##&&(第一条命令成功,则执行后面的命令)
1.txt
7 2.txt
[root@localhost ~]# [ -d aminglinux ] || mkdir aminglinux##存在则不执行,不存在则执行
[root@localhost ~]# ls##[ -d xxx]  判断目录存在与否
1.txt  2.txt  aminglinux  anaconda-ks.cfg  b.txt  test
[root@localhost ~]# [ -d aminglinux ] && mkdir aminglinux##存在则执行,不存在则不执行
mkdir: 无法创建目录"aminglinux": 文件已存在


2018-1-12 5周5次课

标签:[1]   特殊   执行   匹配   创建   translate   man   文件的   符号   

原文地址:http://blog.51cto.com/11530642/2060391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!