Linux 字符处理命令

1.sort

#排序,默认以字母或者数字顺序进行排序,默认以空白字符为分隔符

选项:

-t #指定分隔符
-k #指定以第几列进行排序
-n #按数值大小进行排序
-r #倒叙排序

案例:

[root@hebin ~]# sort sort.txt        #根据默认进行排序。默认以第一列进行排序
abcsf/1/cs/45
aeger/7/ha/164
ekdsn/4/ls/1
escko/6/hg/7
fesaa/2/fe/4
jbdfe/3/fs/3
vfsef/0/ca/89

[root@hebin ~]# sort -t "/" -k4 sort.txt  #指定/为分隔符,以第三3列进行排序     
ekdsn/4/ls/1
aeger/7/ha/164
jbdfe/3/fs/3
fesaa/2/fe/4
abcsf/1/cs/45
escko/6/hg/7
vfsef/0/ca/89

[root@hebin ~]# sort -t "/" -nk4 sort.txt #以数值的大小进行排序
ekdsn/4/ls/1
jbdfe/3/fs/3
fesaa/2/fe/4
escko/6/hg/7
abcsf/1/cs/45
vfsef/0/ca/89
aeger/7/ha/164

[root@hebin ~]# sort -t "/" -rnk4 sort.txt   #以数值大小倒叙排序
aeger/7/ha/164
vfsef/0/ca/89
abcsf/1/cs/45
escko/6/hg/7
fesaa/2/fe/4
jbdfe/3/fs/3
ekdsn/4/ls/1

[root@whb ~]# cat file.txt 
abc
123
abc
123
de
[root@whb ~]# sort file.txt   #默认将重复的行进行排列在一起
123
123
abc
abc
def

2.uniq

#去重(必须是相邻的重复的行),统计重复次数,它是针对行操作的

选项:

-c #统计重复的行的次数

案例:

[root@hebin ~]# sort  sort.log  |  uniq   -c  
      1 abc/1
      2 abd/11
      2 fger/8
      2 fgrf/2
      1 rgrgh/66
[root@hebin ~]# sort  sort.log  |  uniq   -c  | sort -n
      1 abc/1
      1 rgrgh/66
      2 abd/11
      2 fger/8
      2 fgrf/2
[root@hebin ~]# sort  sort.log  |  uniq   -c  | sort -rn
      2 fgrf/2
      2 fger/8
      2 abd/11
      1 rgrgh/66
      1 abc/1

3.cut

#取列,截取字段

选项:

-d #指定分隔符,默认以tab键为分隔符
-f #取出指定的列,多个用逗号分割或者- 连续多列
-c #截取指定的字符,多个用逗号分割或者- 连续的多个字符

案例:

[root@hebin ~]# cat sort.txt 
abcsf/1/cs/45
jbdfe/3/fs/3
fesaa/2/fe/4
aeger/7/ha/164
aeger/7/ha/164

[root@hebin ~]# cut -d '/' -f4 sort.txt     #以/为分隔符,取出第4列
45
3
4
164
164

[root@hebin ~]# cut -d '/' -f4 sort.txt | sort     #将重复的行排序在一起
1
164
164
3
4

[root@hebin ~]# cut -d '/' -f4 sort.txt | sort | uniq    #取出重复行
1
164
3
4

[root@hebin ~]# cut  -d  ":"  -f7   passwd  | sort   | uniq  -c     #统计重复的次数
      2 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     19 /sbin/nologin
      1 /sbin/shutdown
[root@hebin ~]# cut  -d  ":"  -f7   passwd  | sort   | uniq  -c  | sort -rn #根据次数进行倒序排序
     19 /sbin/nologin
      2 /bin/bash
      1 /sbin/shutdown
      1 /sbin/halt
      1 /bin/sync

[root@hebin ~]# cat sort.log 
abc/1
abd/11
fgrf/2
fger/8
rgrgh/66
fger/8
abd/11
fgrf/2
[root@hebin ~]# cut -d "/"  -f2  sort.log 
1
11
2
8
66
8
11
2
[root@hebin ~]# cut -d "/"  -f2  sort.log  |sort -rn   #给截取字段倒叙排序
66
11
11
8
8
2
2
1

[root@hebin ~]# echo  "I am name  is student QQ is 1238376584"  > qq.txt    #取出姓名和QQ号码
[root@hebin ~]# cat qq.txt
I am name  is student QQ is 1238376584
[root@hebin ~]# cut  -d  " " -f3,8  qq.txt     #取出第三列和第八列,第四列是空,有两个空格
name is
[root@hebin ~]# cut  -d  " " -f3,9  qq.txt 
name 1238376584
[root@hebin ~]# cut  -d  " " -f3-9  qq.txt  #取出连续的多列
name  is student QQ is 1238376584

[root@hebin ~]# cat qq.txt 
I am name  is student QQ is 1238376584
[root@hebin ~]# cut  -c  1-3  qq.txt    #截取第一个到第三个字符
I a
[root@hebin ~]# cut  -c  3  qq.txt  #截取第三个字符
a
[root@hebin ~]# cut  -c  1,3  qq.txt    #截取第一个和第三个字符
Ia

4.tr

#替换,删除字符,只能 单对单 的替换

选项:

-d #删除字符

案例:

[root@hebin ~]# cut  -d ","  -f1,2  qq.txt   
I am name  is student QQ is,1238376584
[root@hebin ~]# cut  -d " "  -f3,8  qq.txt  
name is,1238376584
[root@hebin ~]# cut  -d " "  -f3,8  qq.txt   | tr  ","  " "     #将逗号替换为空格
name is 1238376584
[root@hebin ~]# cut  -d " "  -f3,8  qq.txt   | tr  ","  " "  |cut -d " " -f1,3
name 1238376584
[root@hebin ~]# tr  ","  " "  qq.txt 
tr: extra operand ‘qq.txt’
Try 'tr --help' for more information.
[root@hebin ~]# tr  ","  " " < qq.txt       #单独针对文件操作时,必须使用 <
I am name  is student QQ is 1238376584
[root@hebin ~]# tr  ","  " " < qq.txt  |cut -d " " -f3,9
name 1238376584


[root@hebin ~]# tr  "a"  "1"  < qq.txt      #将所有的a替换为1
I 1m n1me  is student QQ is,1238376584
[root@hebin ~]# tr  -d "m"  <qq.txt         #删除m这个字符
I a nae  is student QQ is,1238376584

#取出ip地址

[root@hebin ~]# ip a s  eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ae:0b:8a brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::c653:602a:38c6:e45/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@hebin ~]# ip a s  eth0  | head -3 |tail -1
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[root@hebin ~]# ip a s  eth0  | grep -w 'inet'
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[root@hebin ~]# ip a s  eth0  | grep -w 'inet' | tr  "/"  " "
    inet 10.0.0.100 24 brd 10.0.0.255 scope global noprefixroute eth0
[root@hebin ~]# ip a s  eth0  | grep -w 'inet' | tr  "/"  " " |cut -d " "  -f6
10.0.0.100

#取出IP地址
[root@hebin ~]# ip a s  eth0  | grep -w  'inet'
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
[root@hebin ~]# ip a s  eth0  | grep -w  'inet' |cut -d " "  -f6
10.0.0.100/24
[root@hebin ~]# ip a s  eth0  | grep -w  'inet' |cut -d " "  -f6 | cut  -d "/"  -f1
10.0.0.100

#取出ip地址
[root@hebin ~]# ifconfig  eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::c653:602a:38c6:e45  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ae:0b:8a  txqueuelen 1000  (Ethernet)
        RX packets 3484131  bytes 4986775944 (4.6 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 364363  bytes 53078581 (50.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hebin ~]# ifconfig  eth0 |grep -w inet 
        inet 10.0.0.100  netmask 255.255.255.0  broadcast 10.0.0.255
[root@hebin ~]# ifconfig  eth0 |grep -w inet  | cut -d " "  -f10
10.0.0.100

#取出当前系统登录的用户的次数
[root@hebin ~]# w -h
root     pts/0    10.0.0.1         19:10    9:03   0.01s  0.01s -bash
root     pts/1    10.0.0.1         19:10    9:03   0.00s  0.00s -bash
root     pts/2    10.0.0.1         Fri20    3.00s  0.95s  0.00s w -h
[root@hebin ~]# w -h | cut  -d " " -f1
root
root
root
[root@hebin ~]# w -h | cut  -d " " -f1 |uniq -c
      3 root
[root@hebin ~]# w -h | cut  -d " " -f1 |uniq -c
      3 root
      1 test
[root@hebin ~]# w  
 19:21:39 up 6 days, 32 min,  4 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.0.0.1         19:10   43.00s  0.01s  0.01s -bash
root     pts/1    10.0.0.1         19:10   11:03   0.00s  0.00s -bash
root     pts/2    10.0.0.1         Fri20    3.00s  0.95s  0.00s w
test     pts/3    10.0.0.1         19:21   21.00s  0.00s  0.00s -bash
[root@hebin ~]# w  | more  +3
root     pts/0    10.0.0.1         19:10   50.00s  0.01s  0.01s -bash
root     pts/1    10.0.0.1         19:10   11:10   0.00s  0.00s -bash
root     pts/2    10.0.0.1         Fri20    2.00s  0.96s  0.00s w
test     pts/3    10.0.0.1         19:21   28.00s  0.00s  0.00s -bash

5.wc

#统计字节大小,行数,列数,字符长度

选项:

-l #统计行数
-c #统计总字节大小
-w #统计总列数,默认以空白字符为分隔符
-L #统计最长的一行内容长

案例:

[root@hebin ~]# wc   hosts      #显示所有的信息,总行数,总列数,总字节大小
  2  10 158 hosts
[root@hebin ~]# ll hosts
-rw-r--r--. 1 root root 158 Dec  2 19:26 hosts
[root@hebin ~]# cat hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@hebin ~]# wc -l  hosts        #显示总行数
2 hosts
[root@hebin ~]# wc -c  hosts        #显示总字节大小
158 hosts
[root@hebin ~]# wc -w  hosts        #显示总的列数,默认以空白字符为分隔符
10 hosts
[root@hebin ~]# wc -L hosts     #统计最长一行的字符数量,长度  
78 hosts

[root@hebin ~]# name=12423145637892     #定义一个变量  
[root@hebin ~]# echo $name              #打印变量
12423145637892
[root@hebin ~]# echo $name |wc -L           #统计变量的长度
14
点赞

发表回复