第十课Shell基础知识(下)

8.10shell特殊符_cut命令

Linuxshell中的特殊符号

1、*代表零个或多个任意字符。

[root@localhosttest]# ls

1.txt  1.txt.zip test1  test111  test2 test3

[root@localhosttest]# ls test*

test1:

test111:

test2:

test3:

2、?只代表一个任意的字符。

[root@localhosttest]# ls test?

test1:

test2:

test3:

[root@localhosttest]# ls

1.txt  1.txt.zip test1  test111  test2 test3

3、注释符号#,#后面的内容都会被忽略。

[root@localhosttest]# abc=123 #fadsfadsf

[root@localhosttest]# echo abc

abc

4、脱义字符\,这个字符会将后面的特殊符号(如*)还原为普通字符。

[root@localhosttest]# ls test*

test1:

test111:

test2:

test3:

[root@localhosttest]# ls test\*

ls:无法访问test*:没有那个文件或目录

5、管道符|,表示将前面的输出作为后面命令的输入。

[root@localhosttest]# cat 1.txt |wc -l

4

命令cut,表示用来截取某一个字段,格式cut –d ‘分隔字符’[-cf]n,这里的n是数字。

选项:-d表示后面跟分隔字符,分隔字符要用单引号括起来。

-c表示后面接的是第几个字符

-f表示后面接的是第几个区块。

实例1、[root@localhost test]# cat 1.txt|cut -d':' -f 1|head -5root

bin

daemon

adm

lp

-d选项后面加:冒号,作为分隔字符,-f 1表示截取第一段,-f和1之间空格可有可无。

实例2、[root@localhost test]# head -n2 1.txt|cut-c2

o

i

[root@localhosttest]# head -n2 1.txt|cut -c2-10

oot:x:0:0

in:x:1:1:

[root@localhosttest]# head -n2 1.txt|cut -c2,4,6

otx

i::

-c选项后面可以是一个数字n,也可以是一个区间n1-n2,还可以是多个数字n1,n2,n3.

8.11sort_wc_uniq命令

表示用作排序,命令格式: sort [-t 分隔符][-kn1,n2][-nru],n1,n2指的是数字。

-t:后面跟的是分隔字符

-n:表示使用纯数字排序。

-r:表示反向排序

-u:表示去重复

-kn1,n2:表示由n1区间排序到n2区间,可以只写-k1,即对n1字段排序。

实例1、sort不加任何选项,则从首字符向后依次按ASCII码值进行比较,最后将他们按升序输出:

[root@localhosttest]# head -n5 1.txt |sort

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

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

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

实例2、-t选项后面跟分隔符,-k选项后面跟单个数字表示对第几个区域的字符串排序,-n表示使用纯数字排序:

[root@localhosttest]# head -n5 1.txt |sort -t: -k3 -n

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

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

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

实例2、-k选项后面跟数字n1和n2区域内的字符串排序,-r选项则表示反向排序:

[root@localhosttest]# head -n5 1.txt |sort -t: -k3,5 -r

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

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

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

wc命令用于统计文档的行数,字符数或词数。常用选项有-l(统计行数),-m(统计字符数),-w(统计词数),实例:

[root@localhosttest]# wc 1.txt

  44   782237 1.txt

[root@localhosttest]# wc -l !$

wc-l 1.txt

441.txt

[root@localhosttest]# wc -m 1.txt

22371.txt

[root@localhosttest]# wc -w 1.txt

781.txt

命令uniq用来删除重复的行,该命令只有-c选项比较常用,表示统计重复的行数,并把行数写在前面。实例:

root@localhosttest]# sort 2.txt |uniq

11111

22222

33333

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

      2 11111

      1 22222

      1 33333

8.12tee_tr_split命令

tee命令后面跟文件名,作用类似于重定向>,原文件内容会消失,但会在把文件写入后面跟的文件时,还显示在屏幕上,该命令常用于管道符|后面,实例:

[root@localhosttest]# echo "fadfadfasdfasd" |tee 2.txt

fadfadfasdfasd

[root@localhosttest]# cat 2.txt

Fadfadfasdfasd

命令tr用于替换字符,常用来处理文档中的特殊符号,有常用2个选项:

-d:表示删除某个字符,后面跟要删除的字符

-s:表示删除重复的字符。

实例1、用于把小写字母变为大写字母。

[root@localhosttest]# head -n1 2.txt |tr '[a-z]' '[A-Z]'

FADFADFASDFASD

[root@localhosttest]# cat 2.txt

Fadfadfasdfasd

实例2、还可以替换字符:

[root@localhosttest]# cat 2.txt

fadfadfasdfasd

[root@localhosttest]# head -n1 2.txt |tr 'f' 'W'

WadWadWasdWasd

此命令时针对一个字符来讲的,有一定局限性,如果针对一个字符串,就不能使用了。

命令split常用于分割文档,常用的选项为-b和-l。

-b表示依据大小来分割文档,单位byte:

[root@localhosttest]# ls

1. txt  2.txt test1  test111  test111.tar test111.tar.gz  test.tar  xaa xab  xac  xad xae

指定目标文件名:

[root@localhosttest]# split -b 500 1.txt zanghao

[root@localhosttest]# ls

1.txt  test1   test111.tar     test.tar   zanghaoab zanghaoad

2.txt  test111 test111.tar.gz  zanghaoaa  zanghaoac zanghaoae

选项-l,表示依据行数来分割文档。

[root@localhosttest]# split -l 10 1.txt

[root@localhosttest]# wc -l

^C

[root@localhosttest]# wc -l *

     44 1.txt

      1 2.txt

wc:test1: 是一个目录

      0 test1

wc:test111: 是一个目录

      0 test111

      1 test111.tar

      2 test111.tar.gz

      2 test.tar

     10 xaa

     10 xab

     10 xac

     10 xad

      4 xae

     94 总用量

8.13shell特殊符号下

1、符号$表示变量前面的标志符

2、符号;,当想在一行中运行两个或两个以上的命令,则需要在命令之间加符号;,如下:

[root@localhosttest1]# touch a.txt;touch 2.txt

[root@localhosttest1]# ls

2. txt  a.txt

3、特殊符号~,表示加目录,root用户的家目录是/root,普通用户则是/home/username.

[root@localhost~]# su aming

[aming@localhostroot]$ cd ~

[aming@localhost~]$ pwd

/home/aming

[root@localhost~]# cd ~

[root@localhost~]# pwd

/root

4、特殊符号&,如果想把一条命放到后台执行,通常用于命令运行时间较长的情况。

5、[]中括号内为字符组合,代表字符组合中的任意一个,也可以是一个范围如[1-3],[2-4].

6、特殊符号&&,和||,用于多条命令中间的特殊符号。

使用;,不管command1是否执行成功,都会执行后面的命令

使用&&,只有前面命令执行成功后,后面的命令才会执行,都在后面的命令不执行。实例:

[root@localhosttest1]# ls 1.txt && ls 2.txt

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

[root@localhosttest1]# ls 2.txt && ls1.txt

2.txt

使用||,总有一条命令会执行。实例:

[root@localhosttest1]# ls 1.txt || ls 2.txt

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

2.txt

扩展
关于PROMPT_COMMAND环境变量的含义 http://www.linuxnote.org/prompt_command-environment-variables.html
source exec
区别 http://alsww.blog.51cto.com/2001924/1113112
Linux
特殊符号大全http://ask.apelearn.com/question/7720
sort
并未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975

相关测验题目(必须要做):http://ask.apelearn.com/question/5437
简易审计系统(必须要预习):http://www.68idc.cn/help/server/linux/2014042190951.html

http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html

http://blog.lishiming.net/?p=484

猜你喜欢

转载自blog.csdn.net/zanghaos/article/details/80560277