shell基础(下) 特殊符号

Linux shell中的特殊符号shell

一 cut命令

cut命令: 截取某一个字段。bash

格式:cut -d ‘分隔符’ [-cf] n , n指数字。spa

有以下选项:code

-d : 后跟分隔符。排序

-c: 后面接第几个字符文档

-f: 后接第几个区块字符串

实例:it

[root@localhost ~]# cat /etc/passwd |head -2 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ':' -f 1,2,3
root:x:0
bin:x:1
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ':' -f 1-4
root:x:0:0
bin:x:1:1

 

二 sort命令

用做排序。 class

格式: sort [-t 分隔符]  [-kn1,n2] [-nru]。n1 和n2指数字。test

其余选项的含义以下:

  • -t: 后跟分隔符,做用跟cut 的-d选项同样。
  • -n:表示纯数字排序
  • -r:表示反向排序
  • -u: 去重复
  • -kn1,n2 : 表示由n1区间排序到n2区间,能够只写-kn1,即对n1字段排序
  • 若是sort不加任何选项,则从首字符向后依次按ASCII码值进行比较,最后进行升序输出。

实例以下:

[root@localhost ~]# head -n5 /etc/passwd | 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
// ASCII码值排序  a b d l r

 

  • -t 分隔符、-k后单数字表示对第几个区域的字符串排序,-n选项则表示使用纯数字排序。

实例以下:

[root@localhost ~]# head -n5 /etc/passwd | 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
[root@localhost ~]# head -n5 /etc/passwd | 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

//对比便可发现
  • -kn1,n2表示对n1和n2区域内的字符串排序,-r则表示反向排序。实例以下:
[root@localhost ~]# head -n5 /etc/passwd | 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命令

wc命令用于统计文档的行数,字符数或词数。经常使用选项有-l统计行数 、-m统计字符数、-w统计词数。

实例以下:

[root@localhost ~]# wc /etc/passwd
  23   43 1080 /etc/passwd

//23行 43个词 1080个字符

[root@localhost ~]# wc -l /etc/passwd
23 /etc/passwd

[root@localhost ~]# wc -m /etc/passwd
1080 /etc/passwd
[root@localhost ~]# wc -w /etc/passwd
43 /etc/passwd

 

四 uniq 命令

用来删除重复行。经常使用选项-c 统计重复的行数。

实例以下:

[root@localhost tmp]# uniq 2.txt
111
222
111
333

注:使用uniq前,必须先给文件排序,不然无论用。

实例以下:

[root@localhost tmp]# sort 2.txt| uniq

111
222
333
[root@localhost tmp]# sort 2.txt| uniq -c 
      1 
      2 111
      1 222
      1 333

但不改变原文件

[root@localhost tmp]# cat 2.txt
111
222
111
333

 

五 tee命令

相似> , 重定向的同时还在屏幕显示。还把文件写入后面跟的文件。

实例以下:

[root@localhost tmp]# echo "aaaabbbb" |tee  2.txt
aaaabbbb
[root@localhost tmp]# cat 2.txt
aaaabbbb

 

六 tr命令


替换字符,用来处理文档中出现的特殊字符。

实例以下:

[root@localhost tmp]# head -n2 /etc/passwd |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
[root@localhost tmp]# head -n2 /etc/passwd |tr '[A-Z]' '[a-z]'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

 

七 split命令

用于切割文档。经常使用选项:-b 和 -l。

-b: 表示根据大小来分隔文档,单位为byte。实例以下:

[root@localhost tmp]# mkdir split
[root@localhost tmp]# cd !$
cd split
[root@localhost split]# cp /etc/passwd ./
[root@localhost split]# ls
passwd
[root@localhost split]# split -b 500 passwd

若是不指定目标文件名,则会以xaa、xab... 这样的文件名存取切割后的文件。

[root@localhost split]# ls
passwd  xaa  xab  xac

也可指定目标文件名。如:123

[root@localhost split]# rm -f xa*
[root@localhost split]# split -b 500 passwd 123
[root@localhost split]# ls
123aa  123ab  123ac  passwd

 

-l :据行数切割文档。实例以下:

[root@localhost split]# split -l 10 passwd 
[root@localhost split]# wc -l *
\  12 123aa
   9 123ab
   2 123ac
  23 passwd
  10 xaa
  10 xab
   3 xac
  69 总用量

 

八 特殊符号

$ 变量前缀,结合!成!$。

[root@localhost split]# ls
123aa  123ab  123ac  passwd  xaa  xab  xac
[root@localhost split]# !$
ls
123aa  123ab  123ac  passwd  xaa  xab  xac

 

; 多个命令之间加符号; 运行多个命令。

[root@localhost split]# ls
123aa  123ab  123ac  passwd  xaa  xab  xac
[root@localhost split]# !$
ls
123aa  123ab  123ac  passwd  xaa  xab  xac
[root@localhost split]# touch 2.txt;ls -l
总用量 28
-rw-r--r--. 1 root root  500 1月  12 23:37 123aa
-rw-r--r--. 1 root root  500 1月  12 23:37 123ab
-rw-r--r--. 1 root root   80 1月  12 23:37 123ac
-rw-r--r--. 1 root root    0 1月  12 23:45 2.txt
-rw-r--r--. 1 root root 1080 1月  12 23:33 passwd
-rw-r--r--. 1 root root  385 1月  12 23:39 xaa
-rw-r--r--. 1 root root  575 1月  12 23:39 xab
-rw-r--r--. 1 root root  120 1月  12 23:39 xac

 

~ 表示用户家目录。root用户的家目录是/root, 普通用户则是/home/username。

实例以下:

[root@localhost split]# cd ~
[root@localhost ~]# pwd
/root

 

& 把命令放到后台执行。

 

|| 和 &&  多个命令间的分隔符。(|| 命令或,&&命令与)

使用&&时,只有命令1 执行成功后,命令2才会执行,不然命令2不执行。

使用|| 时, 命令1执行成功后则命令2不执行,不然执行命令2。

  • &&
[root@localhost ~]# touch test1.txt test2.txt
[root@localhost ~]# ls test3.txt && vi test2.txt 
ls: 没法访问test3.txt: 没有那个文件或目录
[root@localhost ~]# ls test3.txt 
ls: 没法访问test3.txt: 没有那个文件或目录
  • ||
[root@localhost ~]# ls test3.txt || vi test2.txt

(更新中。。。)

相关文章
相关标签/搜索