文本处理工具
来源:互联网 发布:161631人工智能怎么 编辑:程序博客网 时间:2024/06/04 17:51
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
- 文本处理工具
- Linux 文本处理工具
- 文本处理工具
- Linux 文本处理工具
- 文本处理工具
- linux文本处理工具
- Linux文本处理工具
- shell-文本处理工具
- 文本处理工具
- 文本处理工具
- 文本处理工具
- 文本处理工具
- 文本处理工具
- 6文本处理工具
- 文本处理工具
- 文本处理工具
- 文本处理工具
- 第六 文本处理工具
- eclipse,工程/文件目录,相对路径(真心的详细~)
- BZOJ 1449: [JSOI2009]球队收益 拆边费用流
- 【算法题】不使用加减乘除符号计算两数之和a+b
- SharedPreference的使用 第一次进入APP时,进入导航页面,再次进入时直接进入主页面 去除ActionBar
- POJ
- 文本处理工具
- Nginx 源码阅读笔记8 epoll 模块
- 软件测试1
- 561. Array Partition I
- PAT1044 火星数字
- 洛谷p1902 二分答案 +bfs
- Atl Com组件DLL非注册调用
- 机器学习实战第五章Logistic回归照葫芦画瓢算法实践
- eclipse配色方案