04-文本处理工具

取ip

[root@localhost ~]# hostname -I| cut -d " " -f1
192.168.80.171
[root@localhost ~]# ifconfig ens33 |head -n2|grep -v ens33 |tr -s  " " |cut -d " " -f3
192.168.80.171
[root@localhost ~]# ip -4  a  | head -n5 |tail -n1 |awk '{print $2}' |awk -F / '{print $1}'
192.168.80.171
[root@localhost ~]# ifconfig ens33 |awk 'NR==2{print}' |tr -s " "|cut -d " " -f3
192.168.80.171

 

cut -d' ' -f1 access_log  #以空格为分隔符,取出第一列

[root@localhost ~]# seq 10 > a.txt
[root@localhost ~]# echo {a..z} > b.txt

[root@localhost ~]# cat b.txt
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@localhost ~]# tr " " "\n" < b.txt   #将空格转换为换行

[root@localhost ~]# tr " " "\n" < b.txt > bb.txt
[root@localhost ~]# cat bb.txt 
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
[root@localhost ~]# paste a.txt bb.txt 
1    a
2    b
3    c
4    d
5    e
6    f
7    g
8    h
9    i
10   j
    k
    l
    m
    n
    o
    p
    q
    r
    s
    t
    u
    v
    w
    x
    y
    z
[root@localhost ~]# paste -s -d " " a.txt #把列换成行,以空格显示
1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# cat access_log |wc -l  #统计访问次数
55777
[root@localhost ~]# sort -t: -k3 -n /etc/passwd  #以:为分隔符,第三列按数字排序从小到大排序
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# sort -t: -k3 -nr /etc/passwd  #倒序排
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin

分区使用率排序

[root@localhost ~]# df |tail -n +2|grep -v cdrom | tr -s ' ' %|cut -d"%" -f5 |sort -nr
26                          #从第二行               空格转换%      %分割第五      排序
8
1
1
1
0
0
0

 -u去重

[root@localhost ~]# df |tail -n +2|grep -v cdrom | tr -s ' ' %|cut -d"%" -f5 |sort -nru 
26
8
1
0

 挨着的才去重

[root@localhost ~]# cat d.txt
1
2
7
7
8
8
9
2
[root@localhost ~]# uniq d.txt
1
2
7
8
9
2
[root@localhost ~]# uniq -c d.txt  
      1 1
      1 2
      2 7
      2 8  两个8
      1 9
      1 2
[root@localhost ~]# uniq -u d.txt  没合并的
1
2
9
2
[root@localhost ~]# uniq -d d.txt   合并的
7
8

统计日志访问量最高的前三名

[root@localhost ~]# cut -d " " -f1 access_log  |sort |uniq -c|sort -nr |head -n3
   4870 172.20.116.228    空格第一列                   访问次数  按数字降序排序 取前三名
   3429 172.20.116.208
   2834 172.20.0.222

 a.txt怎么做变成b.txt

[root@localhost ~]# diff -u a.txt b.txt 
--- a.txt    2022-03-28 20:05:40.868117132 +0800
+++ b.txt    2022-03-28 20:05:57.494117921 +0800
@@ -1,10 +1 @@
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
+a b c d e f g h i j k l m n o p q r s t u v w x y z

 vim 批量注释,vi不生效

按Control+v进入选择模式

shift I 进入编辑模式 输入# 

esc 两下

删除 Control v ,d 删除

 

:%s/sys/SYS/g  批量替换

gg  跳到行首 dgg 删除

shilt + G  跳到行位  dG 删除

ctrl + A  跳到行尾

ctrl +w 删除词组

esc /image n 搜素替换

posted @ 2022-03-29 17:11  gg888666  阅读(48)  评论(0编辑  收藏  举报