文本处理工具

#####unit6#####
 diff
[root@localhost ~]# vim file
[root@localhost ~]# vim file1
[root@localhost ~]# cat file
hello haha
[root@localhost ~]# cat file1
hello haha
23
[root@localhost ~]# diff file file1               #####比较两文件不同
1a2
> 23

[root@localhost ~]# diff -c file file1             ######显示周围行
*** file    2017-04-29 21:07:29.878274421 -0400
--- file1    2017-04-29 21:07:50.802274421 -0400
***************
*** 1 ****
--- 1,2 ----
  hello haha
+ 23

[root@localhost ~]# diff -u file file1            #####使用统一格式显示
--- file    2017-04-29 21:07:29.878274421 -0400
+++ file1    2017-04-29 21:07:50.802274421 -0400
@@ -1 +1,2 @@
 hello haha
+23

[root@localhost ~]# diff -u file file1 > file.path      #####生成补丁file.path
[root@localhost ~]# cat file.path
--- file    2017-04-29 21:07:29.878274421 -0400
+++ file1    2017-04-29 21:07:50.802274421 -0400
@@ -1 +1,2 @@
 hello haha
+23

[root@localhost ~]# yum install patch -y
Loaded plugins: langpacks
rhel_dvd                                     

[root@localhost ~]# patch file  file.path         ####用补丁修改文件
patching file file
[root@localhost ~]# cat file
hello haha
23
[root@localhost ~]# patch -b file file.path     ####备份原文件
patching file file
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[root@localhost ~]# cat file.orig              ####查看生成新文件
hello haha
23

[root@localhost ~]# cat file                      ####显示原文件
hello haha


grep过滤

[root@localhost ~]# rm -fr /mnt/*
[root@localhost ~]# cp /etc/passwd /mnt
[root@localhost ~]# cd /mnt
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat 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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ovirtagent:x:175:175:RHEV-M Guest Agent:/usr/share/ovirt-guest-agent:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
student:x:1000:1000:Student User:/home/student:/bin/bash

usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

test:root:test             #####编辑内容
root:test:root
root:root:test
TEST:root:ROOT

[root@localhost mnt]# grep test passwd           ####细致过滤有关test内容
test:root:test
root:test:root
root:root:test

[root@localhost mnt]# grep -i test passwd       ####模糊过滤有关test的内容
test:root:test
root:test:root
root:root:test
TEST:root:ROOT

[root@localhost mnt]# grep -i test passwd -v     ####反向过滤,除去有关test的内容
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ovirtagent:x:175:175:RHEV-M Guest Agent:/usr/share/ovirt-guest-agent:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
student:x:1000:1000:Student User:/home/student:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

[root@localhost mnt]# grep -i -E "test|root"  passwd             ####过滤多个关键字
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
root:test:root
root:root:test
TEST:root:ROOT

[root@localhost mnt]# grep -i -E "^test|root"  passwd            #####模糊过滤以test开头或是包含关键字root的内容
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
root:test:root
root:root:test
TEST:root:ROOT

[root@localhost mnt]# grep -i -E "^test"  passwd                  #####模糊过滤以tset开头的内容
test:root:test
TEST:root:ROOT

[root@localhost mnt]# grep -i -E "test$"  passwd                 #####模糊过滤以test结尾的内容
test:root:test
root:root:test

[root@localhost mnt]# grep "test" passwd
test:root:test
root:test:root
root:root:test
[root@localhost mnt]# grep "test" passwd   -c                         #####统计包含test的行数
3
[root@localhost mnt]# grep "test" passwd  | grep -E "^test|test$" -v       ####过滤test不在首尾的内容
root:test:root
[root@localhost mnt]# touch file1
[root@localhost mnt]# touch file2
[root@localhost mnt]# echo westos > file2
[root@localhost mnt]# cat file2
westos
[root@localhost mnt]# grep westos -r /mnt/                 ####递归查找目录里的字符
/mnt/file2:westos
[root@localhost mnt]# grep westos -r /mnt/ -n           ####递归查找字符并显示其出现次数
/mnt/file2:1:westos



cut剪切文件

[root@localhost mnt]# cut -d : -f 1 passwd                ######-d指定分隔符,-f指定字段
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
avahi
avahi-autoipd
rpc
rpcuser
nfsnobody

[root@localhost mnt]# cut -c 2-5 passwd                              #####-c指定文本列,剪切第二到第五列
oot:
in:x
aemo
dm:x
p:x:
ync:
hutd
alt:
ail:
pera
ames
tp:x
obod
bus:
olki
vahi
vahi
pc:x
pcus
fsno
virt
ostf
shd:
hron
tude
sbmu
olor
brt:
ibst
nbou
emu:
asla
tp:x
tkit
advd

[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v | awk -F " " '{print $2}'          ######只显示设备id
172.25.254.1


sort排序
[root@localhost mnt]# vim file                  #####编写文件
[root@localhost mnt]# sort file                 ###默认第一列从小到大排序
1
2
3
3
34
4
4
47
5
55
6
6
64
8
8
9

[root@localhost mnt]# sort -n file                    #####按升序排列
1
2
3
3
4
4
5
6
6
8
8
9
34
47
55
64

[root@localhost mnt]# sort -rn file                     ####按降序排列
64
55
47
34
9
8
8
6
6
5
4
4
3
3
2
1

[root@localhost mnt]# sort -rnu file               #####无重复按降序排列
64
55
47
34
9
8
6
5
4
3
2
1

[root@localhost mnt]# sort -rn file | uniq -c             #####统计每个字符行数
      1 64
      1 55
      1 47
      1 34
      1 9
      2 8
      2 6
      1 5
      2 4
      2 3
      1 2
      1 1

[root@localhost mnt]# sort -rn file | uniq -d                     #######显示重复行字符
8
6
4
3

[root@localhost mnt]# sort -rn file | uniq -u                   ######显示唯一行
64
55
47
34
9
5
2
1


[root@localhost mnt]# vim file
[root@localhost mnt]# sort file                  ####默认按照第一列字符升序排列
0:a:47
1:a:3
2:a:4
3:a:5
3:a:6
3:a:8
4:a:34
4:a:4
4:a:55
5:a:3
5:a:6
6:a:64
7:a:1
7:a:9
8:a:2
8:a:8

[root@localhost mnt]# sort -t : -k 3 -n file            #####按照第三列字符升序排列
7:a:1
8:a:2
1:a:3
5:a:3
2:a:4
4:a:4
3:a:5
3:a:6
5:a:6
3:a:8
8:a:8
7:a:9
4:a:34
0:a:47
4:a:55
6:a:64

[root@localhost mnt]# sort -t : -k 3 -n file | uniq -c             ####统计第三列字符重复次数
      1 7:a:1
      1 8:a:2
      1 1:a:3
      1 5:a:3
      1 2:a:4
      1 4:a:4
      1 3:a:5
      1 3:a:6
      1 5:a:6
      1 3:a:8
      1 8:a:8
      1 7:a:9
      1 4:a:34
      1 0:a:47
      1 4:a:55
      1 6:a:64



显示当前进程中内存占据前五的进程的pid:
[root@localhost mnt]# ps ax -o pid --sort -%mem | grep -v PID | head -n 5
 2116
  780
 2051
 2186
  664


[root@localhost mnt]# ls
file  file1  file2  passwd
[root@localhost mnt]# vim file3
[root@localhost mnt]# tr 'a-z' 'A-Z' <file3         ####字符小写变大写
WESTOS
WESTOS

[root@localhost mnt]# tr 'A-Z' 'a-z' <file3         ####字符大写变小写
westos
westos


SED命令
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat passwd            #####查看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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin


[root@localhost mnt]# sed 's/sbin/westos/g' passwd              #####将文件内sbin换为westos
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/westos/nologin
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
[root@localhost mnt]# sed 's/westos/sbin/g' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

[root@localhost mnt]# sed 's/sbin/westos/g' -i passwd               ######将结果输入到文件内
[root@localhost mnt]# sed 's/westos/sbin/g' -i passwd
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost mnt]# sed -e  's/sbin/westos/g'  -e 's/nologin/wang/g' -i passwd       ####同时转变sbin和nologin两个字符
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/westos/wang
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/wang

[root@localhost mnt]# sed -e  's/westos/sbin/g'  -e 's/wang/nologin/g' -i passwd         
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost mnt]# sed -e  's/sbin/westos/g'  -e 's/nologin/wang/g' -i passwd

[root@localhost mnt]# vim rule          ####编写rule
[root@localhost mnt]# cat rule
s/westos/sbin/g
s/wang/nologin/g

[root@localhost mnt]# sed -f rule passwd               ######按照rule编写内容转换
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

shutdown:x:6:0:shutdo

[root@localhost mnt]# sed -f rule -i  passwd        #####把转换后的内容输入到指定文件
[root@localhost mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@localhost mnt]# cat passwd -b > westos                 ####输出显示行号
[root@localhost mnt]# cat westos
     1    root:x:0:0:root:/root:/bin/bash
     2    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     3    sync:x:5:0:sync:/sbin:/bin/sync
     4    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     5    halt:x:7:0:halt:/sbin:/sbin/halt
     6    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
     7    operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost mnt]# sed '3,5s/sbin/wang/g' passwd             #####将3-5行的字符sbin换为字符wang
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/wang:/wang/nologin
adm:x:3:4:adm:/var/adm:/wang/nologin
lp:x:4:7:lp:/var/spool/lpd:/wang/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@localhost mnt]# sed 5p westos                          #######重复第五行
     1    root:x:0:0:root:/root:/bin/bash
     2    bin:x:1:1:bin:/bin:/sbin/nologin
     3    daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4    adm:x:3:4:adm:/var/adm:/sbin/nologin
     5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6    sync:x:5:0:sync:/sbin:/bin/sync
     7    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8    halt:x:7:0:halt:/sbin:/sbin/halt
     9    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10    operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost mnt]# sed -n 5p westos              #########只显示第五行
     5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost mnt]#sed -ne 2p -ne 5p westos   ####显示第二行和第四行
     2    bin:x:1:1:bin:/bin:/sbin/nologin
     5    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值