Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141602
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-04 11:19
文章分类

全部博文(70)

文章存档

2018年(69)

2016年(1)

我的朋友

分类: LINUX

2018-07-03 23:40:37

8.10  shell特殊符_cut命令
* :任意个任意字符
?:任意一个字符
#:注释字符。即使是在命令行输入的命令前面加个#也是注释
\ :脱义字符
| :管道符

注意这里关于脱义字符的使用:

点击(此处)折叠或打开

  1. [root@localhost00m:~# a=1
  2. [root@localhost00m:~# b=2
  3. [root@localhost00m:~# c=$a$b
  4. [root@localhost00m:~# echo $c
  5. 12
  6. [root@localhost00m:~# c='$a$b'
  7. [root@localhost00m:~# echo $c
  8. $a$b
  9. [root@localhost00m:~# c=\$a\$b
  10. [root@localhost00m:~# echo $c
  11. $a$b
管道符相关的内容:


cut的例子:

点击(此处)折叠或打开

  1. [root@localhost00m:~]# cat /etc/passwd | head -2
  2. root:x:0:0:root:/root:/bin/bash
  3. bin:x:1:1:bin:/bin:/sbin/nologin
  4. [root@localhost00m:~]# cat /etc/passwd | head -2 | cut -d ":" -f 1,2
  5. root:x
  6. bin:x
  7. [root@localhost00m:~]# cat /etc/passwd | head -2 | cut -c 4
    t
    :
注解:cut命令的选项:
“-d”:指定分隔符
“-f”:指定截取哪些字段。比如例子中指定截取  1和2段。如果是1到3段,则是:“-f  1-3”
“-c”:指定第几个字符。也可以是范围“-c  4-6”

8.11  sort_wc_uniq命令
sort:排序,经常和uniq组合一起使用

点击(此处)折叠或打开

  1. [root@localhost00m:~]# cat /etc/passwd | head -2
  2. root:x:0:0:root:/root:/bin/bash
  3. bin:x:1:1:bin:/bin:/sbin/nologin
  4. [root@localhost00m:~]# sort /etc/passwd | head -2
  5. adm:x:3:4:adm:/var/adm:/sbin/nologin
  6. aiya:x:1102:1102::/www/aiya:/sbin/nologin
sort:默认按照ASCII码排序。默认升序,从小到大

点击(此处)折叠或打开

  1. [root@localhost00m:~]# cat a.txt
  2. [
  3. {
  4. ?
  5. /
  6. |
  7. aaa
  8. bb
  9. c
  10. 1
  11. 3
  12. A
  13. C
  14. [root@localhost00m:~]# sort a.txt
  15. |
  16. ?
  17. /
  18. [
  19. {
  20. 1
  21. 3
  22. A
  23. aaa
  24. bb
  25. c
  26. C
选项:
“-n”:以数字为标准排序,特殊字符字母等,会被标记为0。
“-r”:倒序排列。即降序,由大到小排序。

wc -l 统计行数,-m 统计字符数(包括隐藏字符:换行符), -w 统计词(以空格作为分隔)

点击(此处)折叠或打开

  1. [root@localhost ~]# cat a.txt
  2. 123
  3. 456
  4. [root@localhost ~]# wc -m a.txt
  5. 8 a.txt
  6. [root@localhost ~]# cat -A a.txt        //查看文件所有内容,包括隐藏
  7. 123$
  8. 456$
  9. [root@localhost ~]#

uniq:去重。-c 统计去重的次数,即该字符重复次数

点击(此处)折叠或打开

  1. [root@localhost ~]# cat a.txt
  2. 123
  3. 456
  4. abc def
  5. 123 456
  6. 456
  7. 789
  8. 987
  9. [root@localhost ~]# uniq a.txt
  10. 123
  11. 456
  12. abc def
  13. 123 456
  14. 456
  15. 789
  16. 987
  17. [root@localhost ~]# vim a.txt
  18. [root@localhost ~]# uniq a.txt
  19. 123
  20. 456
  21. abc def
  22. 123 456
  23. 456
  24. 789
  25. 987
  26. [root@localhost ~]# cat a.txt
  27. 123
  28. 456
  29. abc def
  30. 123 456
  31. 456
  32. 789
  33. 987
  34. 987

去重事有条件的:需要先排序,然后再去重。所以经常和sort结合使用。

点击(此处)折叠或打开

  1. [root@localhost ~]# sort a.txt
  2. 123
  3. 123 456
  4. 456
  5. 456
  6. 789
  7. 987
  8. 987
  9. abc def
  10. [root@localhost ~]# sort a.txt | uniq
  11. 123
  12. 123 456
  13. 456
  14. 789
  15. 987
  16. abc def
排序再去重,然后统计:

点击(此处)折叠或打开

  1. [root@localhost ~]# sort a.txt | uniq -c
  2. 1 123
  3. 1 123 456
  4. 2 456
  5. 1 789
  6. 2 987
  7. 1 abc def

8.12 tee_tr_split命令
tee和>类似,重定向的同时,还在屏幕显示

点击(此处)折叠或打开

  1. [root@localhost ~]# sort a.txt | uniq -c > b.txt
  2. [root@localhost ~]# vim b.txt
  3. [root@localhost ~]# cat b.txt        //必须查看才知道是否写入成功
  4. 1 123
  5. 1 123 456
  6. 2 456
  7. 1 789
  8. 2 987
  9. 1 abc def
那么这个时候,就可以使用tee命令:

点击(此处)折叠或打开

  1. [root@localhost ~]# sort a.txt | uniq -c | tee c.txt
  2. 1 123
  3. 1 123 456
  4. 2 456
  5. 1 789
  6. 2 987
  7. 1 abc def
  8. [root@localhost ~]# cat c.txt 
          1 123
          1 123 456
          2 456
          1 789
          2 987
          1 abc def

直接使用tee命令是覆盖,如果有追加的需求,需要使用 选项“-a”


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

点击(此处)折叠或打开

  1. [root@localhost ~]# echo "kgc-yunjisuan" | tr 'kyj' 'KYJ'
  2. Kgc-YunJisuan
  3. [root@localhost ~]# echo "kgc-yunjisuan" | tr '[kgc]' '[KGC]'
  4. KGC-yunjisuan
  5. [root@localhost ~]# echo "kgc-yunjisuan" | tr '[a-z]' '[A-Z]'
  6. KGC-YUNJISUAN
  7. [root@localhost ~]# echo "kgc-yunjisuan" | tr '[a-z]' '[1]'
  8. ]]]-]]]]]]]1]
  9. [root@localhost ~]# echo "kgc-yunjisuan" | tr '[a-z]' '1'
  10. 111-111111111
  11. [root@localhost ~]#

split:切割,-b 大小(默认单位字节),-l 行数
将大文件,切割成小文件

点击(此处)折叠或打开

  1. [root@localhost tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> text.txt \;
  2. [root@localhost tmp]# du -sh text.txt
  3. 248K text.txt
  4. [root@localhost tmp]# rm -rf two/*
  5. [root@localhost tmp]# mv text.txt two/
  6. [root@localhost tmp]# cd two/
  7. [root@localhost two]# split -b 1000 text.txt
  8. [root@localhost two]# ls
  9. text.txt xaq xbh xby xcp xdg xdx xeo xff xfw xgn xhe xhv xim xjd
  10. xaa xar xbi xbz xcq xdh xdy xep xfg xfx xgo xhf xhw xin xje
  11. xab xas xbj xca xcr xdi xdz xeq xfh xfy xgp xhg xhx xio xjf
  12. xac xat xbk xcb xcs xdj xea xer xfi xfz xgq xhh xhy xip xjg
  13. xad xau xbl xcc xct xdk xeb xes xfj xga xgr xhi xhz xiq xjh
  14. xae xav xbm xcd xcu xdl xec xet xfk xgb xgs xhj xia xir xji
  15. xaf xaw xbn xce xcv xdm xed xeu xfl xgc xgt xhk xib xis xjj
  16. xag xax xbo xcf xcw xdn xee xev xfm xgd xgu xhl xic xit xjk
  17. xah xay xbp xcg xcx xdo xef xew xfn xge xgv xhm xid xiu xjl
  18. xai xaz xbq xch xcy xdp xeg xex xfo xgf xgw xhn xie xiv xjm
  19. xaj xba xbr xci xcz xdq xeh xey xfp xgg xgx xho xif xiw xjn
  20. xak xbb xbs xcj xda xdr xei xez xfq xgh xgy xhp xig xix xjo
  21. xal xbc xbt xck xdb xds xej xfa xfr xgi xgz xhq xih xiy **
  22. xam xbd xbu xcl xdc xdt xek xfb xfs xgj xha xhr xii xiz xjq
  23. xan xbe xbv xcm xdd xdu xel xfc xft xgk xhb xhs xij xja
  24. xao xbf xbw xcn xde xdv xem xfd xfu xgl xhc xht xik xjb
  25. xap xbg xbx xco xdf xdw xen xfe xfv xgm xhd xhu xil xjc
然后查看每个文件的大小,是否是按照我们的要求进行的:
[root@localhost two]# du -sb *
250672 text.txt
1000 xaa
1000 xab
1000 xac
......        //没有指定文件名,那么系统按照这样类似的结构命令。

那么如果想要指定文件名,可以通过指定文件名前缀的方式进行:

点击(此处)折叠或打开

  1. [root@localhost two]# split -b 100k text.txt
  2. [root@localhost two]# du -sh *
  3. 248K text.txt
  4. 100K xaa
  5. 100K xab
  6. 48K xac
  7. [root@localhost two]# split -b 100k text.txt abc.
  8. [root@localhost two]# du -sh *
  9. 100K abc.aa
  10. 100K abc.ab
  11. 48K abc.ac
  12. 248K text.txt
  13. 100K xaa
  14. 100K xab
  15. 48K xac
当然,也可以通过行来划分:

点击(此处)折叠或打开

  1. [root@localhost two]# wc -l text.txt
  2. 6289 text.txt
  3. [root@localhost two]# split -l 1000 text.txt
  4. [root@localhost two]# wc -l *
  5. 6289 text.txt
  6. 1000 xaa
  7. 1000 xab
  8. 1000 xac
  9. 1000 xad
  10. 1000 xae
  11. 1000 xaf
  12. 289 xag
  13. 12578 总用量

8.13 shell特殊符号(下)
$变量前缀,!$组合,正则当中表示行尾
;多行命令写道一行,用分号分隔

点击(此处)折叠或打开

  1. [root@localhost two]# for i in `seq 1 10`
  2. > do
  3. > echo $i
  4. > done
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. 6
  11. 7
  12. 8
  13. 9
  14. 10
  15. [root@localhost two]# for i in `seq 1 10`; do echo $i; done    //通过向上的方向键,调回执行过的历史命令。发现中间是使用分号分隔。

~ 用户家目录,正则表达式 表示匹配符
& 放在命令后面,会吧命令丢到后台
“>”:正确重定向,覆盖之前的文件内容
“>>”:追加重定向,正确的内容,追加到文件末尾
“2>”:错误重定向,覆盖之前的文件内容
“2>>”:追加重定向,错误的内容,追加到文件末尾
“&>”:正确和错误的信息都输入到文件当中
[] 指定字符中的一个,[0-9],[a-zA-Z],[abc]
||和&& ,用在命令之间

点击(此处)折叠或打开

  1. [root@localhost ~]# ls aaa.txt ; wc -l a.txt
  2. ls: 无法访问aaa.txt: 没有那个文件或目录
  3. 8 a.txt
  4. [root@localhost ~]# ls aaa.txt || wc -l a.txt    //前面命令不成功,则执行后面的命令。
  5. ls: 无法访问aaa.txt: 没有那个文件或目录
  6. 8 a.txt
  7. [root@localhost ~]# ls aaa.txt && wc -l a.txt      //只有前面命令成功,才执行后面的命令
  8. ls: 无法访问aaa.txt: 没有那个文件或目录
  9. [root@localhost ~]# ls a.txt || wc -l a.txt       //前面命令执行成功,后面命令不再执行
  10. a.txt
一般可以用来表示判断,继而进行操作过程。

点击(此处)折叠或打开

  1. [root@localhost ~]# [ -d kgc ] || mkdir kgc
  2. [root@localhost ~]# [ -d kgc ] || mkdir kgc    //重复执行不会报错。
  3. [root@localhost ~]# [ -d kgc ] && mkdir kgc
  4. mkdir: 无法创建目录"kgc": 文件已存在
  5. [root@localhost ~]# ls kgc -d
  6. kgc

阅读(1215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~