linux-文本查看和处理命令

一、文本处理三剑客介绍

通配符简单说明
*:匹配任意个字符
?:匹配任意一个字符
[abcd]:abcd四个字母中的一个
[^abcd]:abcd四个字母以外的其它字母
[0-9]:匹配0-9数字
[[:digit:]] :匹配数字
[[:lower:]]:匹配小写字母
[[:upper:]]:匹配大写字母
[[:alpha:]]:匹配字母

grep、sed、awk都支持正则表达式
grep:主要用于搜索某些字符串;
sed:用于处理文本 ,修改文本内容;
awk:用于处理文本 ,修改文本内容;

二、查看文本的命令

linux-文本查看和处理命令_第1张图片
image.png

linux-文本查看和处理命令_第2张图片
image.png

2.1、cat、tac、rev、head、tail命令

[root@localhost /home/unnet/data]#cat -n txt.txt 
     1  查明;测定;准确算出 
     2  to discover the facts about sth; to calculate sth exactly
     3  
     4  同义词: establish
     5  [VN] An inquiry was set up to determine the cause of the accident.
     6  
     7  已展开调查以确定事故原因。
     8  
     9  [V wh-] We set out to determine exactly what happened that night.
    10  
    11  我们着手查明那天晚上发生的事情。
    12  
[root@localhost /home/unnet/data]#tac txt.txt 

我们着手查明那天晚上发生的事情。

[V wh-] We set out to determine exactly what happened that night.

已展开调查以确定事故原因。

[VN] An inquiry was set up to determine the cause of the accident.
同义词: establish

to discover the facts about sth; to calculate sth exactly
查明;测定;准确算出 
[root@localhost /home/unnet/data]#echo {a..e}
a b c d e
[root@localhost /home/unnet/data]#echo {a..e} | rev
e d c b a

2.2、more、less命令

linux-文本查看和处理命令_第3张图片
image.png

more和less用于文件内容较多时分页查看;

linux-文本查看和处理命令_第4张图片
image.png
[root@localhost /home/unnet/data]#head -n 5 txt.txt 
查明;测定;准确算出 
to discover the facts about sth; to calculate sth exactly

同义词: establish
[VN] An inquiry was set up to determine the cause of the accident.
[root@localhost /home/unnet/data]#tail -n 5 txt.txt 

[V wh-] We set out to determine exactly what happened that night.

我们着手查明那天晚上发生的事情。

2.3、生成随机口令字符串

[root@localhost ~]#whatis tr
tr (1)               - translate or delete characters

[root@localhost ~]#cat /dev/urandom | tr -dc "a-zA-Z0-9" | head -c 10
85zEIvB20E
[root@localhost ~]#

2.4、cut命令

linux-文本查看和处理命令_第5张图片
image.png
[root@localhost ~]#whatis cut
cut (1)              - remove sections from each line of files
[root@localhost ~]#cut --help
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=LIST        select only these bytes
  -c, --characters=LIST   select only these characters
  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified
  -n                      with -b: don't split multibyte characters
      --complement        complement the set of selected bytes, characters
                            or fields
  -s, --only-delimited    do not print lines not containing delimiters
      --output-delimiter=STRING  use STRING as the output delimiter
                            the default is to use the input delimiter
      --help     display this help and exit
      --version  output version information and exit

Use one, and only one of -b, -c or -f.  Each LIST is made up of one
range, or many ranges separated by commas.  Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:

  N     N'th byte, character or field, counted from 1
  N-    from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M    from first to M'th (included) byte, character or field

With no FILE, or when FILE is -, read standard input.
[root@localhost ~]#head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]#cut -d: -f2 /etc/passwd | head -2
x
x
[root@localhost ~]#cut -d: -f1,2 /etc/passwd | head -2
root:x
bin:x
[root@localhost ~]#cut -d: -f1,2,5-7 /etc/passwd | head -2
root:x:root:/root:/bin/bash
bin:x:bin:/bin:/sbin/nologin

####取磁盘利用率那一列
[root@localhost ~]#df
Filesystem              1K-blocks     Used Available Use% Mounted on
/dev/mapper/centos-root 258994628 49085980 209908648  19% /
devtmpfs                  3992736        0   3992736   0% /dev
tmpfs                     4004628        0   4004628   0% /dev/shm
tmpfs                     4004628   411180   3593448  11% /run
tmpfs                     4004628        0   4004628   0% /sys/fs/cgroup
/dev/sda1                 1038336   189832    848504  19% /boot

[root@localhost ~]#df | tr -s " " % | cut -d% -f5
Use
19
0
0
11
0
19
###取网卡的IP地址
[root@localhost ~]#ifconfig ens192
ens192: flags=4163  mtu 1500
        inet 172.20.4.56  netmask 255.255.255.0  broadcast 172.20.4.255
        inet6 fe80::3417:4b84:e33d:481b  prefixlen 64  scopeid 0x20
        ether 00:0c:29:ff:e1:49  txqueuelen 1000  (Ethernet)
        RX packets 64168615  bytes 35499908325 (33.0 GiB)
        RX errors 0  dropped 606  overruns 0  frame 0
        TX packets 23099415  bytes 10478735183 (9.7 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]#ifconfig ens192 | head -2 | tail -1 | tr -s " " : | cut -d: -f2
inet
[root@localhost ~]#ifconfig ens192 | head -2 | tail -1 | tr -s " " : | cut -d: -f3
172.20.4.56

2.5、paste命令

linux-文本查看和处理命令_第6张图片
image.png
[root@localhost /home/unnet/data]#cat a.txt
172.20.4.56
172.20.4.57
172.20.4.58
172.20.4.59
172.20.4.60
[root@localhost /home/unnet/data]#cat b.txt 
a
b
c
d
e
[root@localhost /home/unnet/data]#whatis paste
paste (1)            - merge lines of files
[root@localhost /home/unnet/data]#paste -d: a.txt b.txt 
172.20.4.56:a
172.20.4.57:b
172.20.4.58:c
172.20.4.59:d
172.20.4.60:e

2.6、wc命令

linux-文本查看和处理命令_第7张图片
image.png
-rw-r--r-- 1 root webs 60 Jul  4 15:48 a.txt
[root@localhost /home/unnet/data]#whatis wc
wc (1)               - print newline, word, and byte counts for each file
###5行 5个单词 60字节
[root@localhost /home/unnet/data]#wc a.txt 
 5  5 60 a.txt
[root@localhost /home/unnet/data]#ll a.txt 

你可能感兴趣的:(linux-文本查看和处理命令)