第三章 文件过滤及内容编辑处理命令
Posted key-oldboy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三章 文件过滤及内容编辑处理命令相关的知识,希望对你有一定的参考价值。
3.1 cat
cat命令作用:
1、查看文件内容
2、把多个文件合并成一个文件
3、编辑文件内容
4、结合>>和<<EOF进行编辑
5、清空文件内容
-n参数 从1开始对所有输出的内容按行编号
-b参数 忽略显示空白行行号
-E参数 在每一行行尾显示$符号(即使是空行 结尾也是有结束标识的)
-s参数 当遇到有连续两行以上的空白行时,就替代为一行空白行
[[email protected] ~]# cat test1.txt test1 [[email protected] ~]# cat test{,1}.txt 同时把test.txt和test1.txt同时读出 相当于两个文件合并成一个文件 my b is http:hahah my c is http:hahah my d is http:hahah ay c is http:hahah by c is http:hahah cy c is http:hahah oldboy OLDBOY oldboy. 000btti000000000000anji000ngingni. shanghaibeijingtiaks00000dkasskjdhshdjslfjslajfhsjhdflkahdlfjaldf shanghaibeijingtiaks00000dkass0000000kjdhshdjslfjslajfhsjhdflkahdlfjaldf shanghaksjdasdashhhhhh0000000000000000hhhhhhhhhhhhhhjssssssssssssssshsasaasdasdasd 0.000000000000000000 test1 [[email protected] ~]# cat test{,1}.txt >/tmp/2018.txt [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# cat /tmp/2018.txt my b is http:hahah my c is http:hahah my d is http:hahah ay c is http:hahah by c is http:hahah cy c is http:hahah oldboy OLDBOY oldboy. 000btti000000000000anji000ngingni. shanghaibeijingtiaks00000dkasskjdhshdjslfjslajfhsjhdflkahdlfjaldf shanghaibeijingtiaks00000dkass0000000kjdhshdjslfjslajfhsjhdflkahdlfjaldf shanghaksjdasdashhhhhh0000000000000000hhhhhhhhhhhhhhjssssssssssssssshsasaasdasdasd 0.000000000000000000 test1 [[email protected] ~]# [[email protected] ~]# cat > /tmp/2018.txt 编辑2018.txt文件内容 I am Linux 2222222 3333333 ^C [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 [[email protected] ~]# [[email protected] ~]# cat >>/tmp/2018.txt<<EOF 交互式进行编辑2018.txt文件内容 > hello word > mingtian nihao > EOF [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao [[email protected] ~]# cat -n 参数的意思是显示行号 空行也会显示行号 [[email protected] ~]# cat -n /tmp/2018.txt 1 I am Linux 2 2222222 3 3333333 4 hello word 5 mingtian nihao [[email protected] ~]# cat -b 参数是 不为空行做标记行号 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -b /tmp/2018.txt cat -b 忽略显示空白行行号 1 I am Linux 2 2222222 3 3333333 4 hello word 5 mingtian nihao 6 nishishuo 7 whoami 8 wo 9 ni r [[email protected] ~]# cat -E参数 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -E /tmp/2018.txt 在每一行行尾显示$符号(包括空行) I am Linux$ 2222222$ 3333333$ hello word $ mingtian nihao$ $ $ nishishuo$ whoami$ wo $ ni r$ [[email protected] ~]# [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -s /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# 生产环境中,用grep进行过滤空行 [[email protected] liangli]# grep -v "^$" test.txt 1 2 3 4 5 6 7 hello word [[email protected] liangli]# cat -T 参数是区分tab键和空格 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -T /tmp/2018.txt I^Iam Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]#
3.2 tac
反向显示文件内容(每行本文顺序没有改变)
[[email protected] liangli]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don‘t # want to do the full Sys V style init stuff. touch /var/lock/subsys/local >/etc/udev/rules.d/70-persistent-net.rules [[email protected] liangli]# tac /etc/rc.local >/etc/udev/rules.d/70-persistent-net.rules touch /var/lock/subsys/local # want to do the full Sys V style init stuff. # You can put your own initialization stuff in here if you don‘t # This script will be executed *after* all the other init scripts. # #!/bin/sh [[email protected] liangli]#
3.3 less、move
less 和more相反 内容一屏一屏的显示(按空格键) 回车的话是一行显示的 按b可以一次回退一屏
more 更多 按页一次一屏 内容一屏一屏的显示(按空格键) 回车的话是一行显示的 不能回退 具有和 vi编辑的一些小功能
= 键可以显示文本有多少行
/mysql 具有搜索的功能
v 键 可以进行编辑了
q 退出more
[[email protected] ~]# more -10 /etc/services 按10行进行显示 [[email protected] ~]# more +10000 /etc/services 直接到10000行 [[email protected] liangli]# ll /etc/ | more -10 用法 总用量 1720 drwxr-xr-x. 3 root root 4096 6月 15 00:28 abrt drwxr-xr-x. 4 root root 4096 6月 15 00:30 acpi -rw-r--r--. 1 root root 48 9月 28 23:11 adjtime -rw-r--r--. 1 root root 1512 1月 12 2010 aliases -rw-r--r--. 1 root root 12288 6月 15 00:33 aliases.db drwxr-xr-x. 2 root root 4096 6月 15 00:30 alsa drwxr-xr-x. 2 root root 4096 6月 15 00:29 alternatives -rw-------. 1 root root 541 3月 30 2015 anacrontab -rw-r--r--. 1 root root 148 5月 15 2009 asound.conf --More-- less命令 分页查看文件 推荐 less -N 可以显示行号 [[email protected] liangli]# ll /etc/ | less -N 1 总用量 1720 2 drwxr-xr-x. 3 root root 4096 6月 15 00:28 abrt 3 drwxr-xr-x. 4 root root 4096 6月 15 00:30 acpi 4 -rw-r--r--. 1 root root 48 9月 28 23:11 adjtime 5 -rw-r--r--. 1 root root 1512 1月 12 2010 aliases 6 -rw-r--r--. 1 root root 12288 6月 15 00:33 aliases.db 7 drwxr-xr-x. 2 root root 4096 6月 15 00:30 alsa 8 drwxr-xr-x. 2 root root 4096 6月 15 00:29 alternatives 9 -rw-------. 1 root root 541 3月 30 2015 anacrontab 10 -rw-r--r--. 1 root root 148 5月 15 2009 asound.conf 11 -rw-r--r--. 1 root root 1 2月 20 2015 at.deny 12 drwxr-x---. 3 root root 4096 6月 15 00:30 audisp 13 drwxr-x---. 3 root root 4096 6月 15 00:30 audit 14 drwxr-xr-x. 2 root root 4096 6月 15 00:30 bash_completion.d 15 -rw-r--r--. 1 root root 2681 10月 2 2013 bashrc 16 drwxr-xr-x. 2 root root 4096 10月 15 2014 blkid 17 drwxr-xr-x. 2 root root 4096 6月 15 00:27 bonobo-activation 18 -rw-r--r--. 1 root root 1780 10月 16 2009 cas.conf 19 -rw-r--r--. 1 root root 27 8月 4 2015 centos-release 20 drwxr-xr-x. 2 root root 4096 3月 10 2015 chkconfig.d 21 drwxr-xr-x. 2 root root 4096 6月 15 00:29 compat-openmpi-psm-x86_64 :
3.4 head
显示文件内容头部 读取文件的前N行 默认是前10行 -n 数字(习惯-5 忽略-n) head -30 liangli.txt |tail -11 这条命令就是取数值20到30之间的数字
-c 参数 显示文件的前五个字节
[[email protected] liangli]# head -10 /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 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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin [[email protected] liangli]# [[email protected] ~]# head -c 5 /etc/inittab # ini[[email protected] ~]# [[email protected] ~]# 将/etc/inittab文件后10行去掉 [[email protected] ~]# head -n -10 /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # [[email protected] ~]# 显示多个文件 [[email protected] liangli]# head /etc/passwd /etc/inittab ==> /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 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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin ==> /etc/inittab <== # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # [[email protected] liangli]#
3.5 tail
显示文件内容尾部 读取文件的后N行 默认情况下为后10行 -n 数字 习惯-5 忽略-n
-f 参数 实时输出日志的动态变化 假如现在在一个窗口中输入 tail -f test.txt 然后在另一个窗口下echo 1111>> test.txt 会看到相应的1111的输出
-F 参数 就是test.txt这个文件事先可以不存在 但是我会等着这个文件的创建后在及时输出相应这个文件的echo内容
tailf 命令和 tail -f 的作用一样 但是 tailf 是一个单独的命令
[[email protected] liangli]# tail -5 /etc/passwd saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin user1:x:500:500::/home/user1:/bin/bash [[email protected] liangli]#
3.6 cut
从文本中提取一段文字并输出
cut -b参数 按字节进行切割 字节切割 不会对中文生效
cut -c 按字符进行切割 字符切割 可以切割中文
cut -d 参数 指定分隔符 d和f一起用 默认情况下cut命令以tab键作为分隔符
[[email protected] ~]# cat test.txt I am oldboy my qq is 1234567 [[email protected] ~]# cut -b 3 test.txt 第3个字节、字符、字段切割 a [[email protected] ~]# cut -b 3-4 test.txt 从第3到4个字节、字符、字段进行切割 am [[email protected] ~]# [[email protected] ~]# cut -b -4 test.txt 从第一到4个字节、字符、字段进行切割 I am [[email protected] ~]# cut -b 4- test.txt 从第4个字节、字符、字段进行切割 m oldboy my qq is 1234567 [[email protected] ~]# cut -c 按字符进行切割 一个中文字符占2个字节 [[email protected] ~]# cat test.txt I am oldboy my qq is 1234567 i清明节放假 [[email protected] ~]# cut -b -4 test.txt I am i清 cut -d 参数 指定分隔符 d和f一起用 默认情况下cut命令以tab键作为分隔符 cut命令不能支持多个字符作为分隔符 [[email protected] ~]# head -1 /etc/passwd root:x:0:0:root:/root:/bin/bash [[email protected] ~]# head -1 /etc/passwd |cut -d : -f1 root [[email protected] ~]# head -1 /etc/passwd |cut -d : -f2 x [[email protected] ~]# head -1 /etc/passwd |cut -d : -f3 0 [[email protected] ~]# head -1 /etc/passwd |cut -d : -f4 0 [[email protected] ~]# [[email protected] ~]# cat /etc/passwd | cut -d : -f 1-3 root:x:0 bin:x:1 daemon:x:2 adm:x:3 lp:x:4 sync:x:5 shutdown:x:6 halt:x:7 mail:x:8 uucp:x:10 operator:x:11 games:x:12 gopher:x:13 ftp:x:14 nobody:x:99 dbus:x:81 vcsa:x:69 abrt:x:173 haldaemon:x:68 ntp:x:38 saslauth:x:499 postfix:x:89 sshd:x:74 tcpdump:x:72 user1:x:500 [[email protected] ~]# [[email protected] ~]# cat -T test.txt this^Iis^Itab^Iline this is ni hao [[email protected] ~]# cut -f 2-3 test.txt is tab this is ni hao [[email protected] ~]# [[email protected] ~]# cat test.txt this is tab line this is ni hao [[email protected] ~]# cut -d ‘ ‘ -f 2-3 test.txt 一个空格 this is tab line is ni [[email protected] ~]# [[email protected] ~]# cut -d ‘ ‘ -f 2-3 test.txt 两个空格 cut: 分界符必须是单个字符 请尝试执行"cut --help"来获取更多信息。 [[email protected] ~]#
3.7 spilt
按照指定的行数或大小分割文件
-l 指定分割文件后最大行数
-a 指定后缀长度,默认2个字母
-d 使用数字后缀
-b 指定分割文件的最大字节数
[[email protected] ~]# wc -l /etc/inittab 26 /etc/inittab [[email protected] ~]# split -l 10 /etc/inittab new_ [[email protected] ~]# [[email protected] ~]# ls new_* new_aa new_ab new_ac [[email protected] ~]# head new_aa new_ab new_ac ==> new_aa <== # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # ==> new_ab <== # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) ==> new_ac <== # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault: -a 指定生成文件后缀长度 [[email protected] ~]# split -l 10 -a 4 /etc/inittab new2_ [[email protected] ~]# wc -l new2_* 10 new2_aaaa 10 new2_aaab 6 new2_aaac 26 总用量 [[email protected] ~]# -d 使用数字后缀 [[email protected] ~]# split -l 10 -d /etc/inittab new3_ [[email protected] ~]# wc -l new3_* 10 new3_00 10 new3_01 6 new3_02 26 总用量 [[email protected] ~]# -b 指定分割大小 [[email protected] 2016]# ll -h 总用量 80K -rw-r--r-- 1 root root 77K 9月 24 20:54 keykey.txt [[email protected] 2016]# [[email protected] 2016]# split -b 20K keykey.txt [[email protected] 2016]# ll -h 总用量 160K -rw-r--r-- 1 root root 77K 9月 24 20:54 keykey.txt -rw-r--r-- 1 root root 20K 9月 24 20:54 xaa -rw-r--r-- 1 root root 20K 9月 24 20:54 xab -rw-r--r-- 1 root root 20K 9月 24 20:54 xac -rw-r--r-- 1 root root 17K 9月 24 20:54 xad [[email protected] 2016]#
3.8 paste
合并文件 能将文件按照行与行进行合并,中间使用tab隔开
-d 参数 指定合并的分隔符 默认是Tab
-s 参数 每个文件占用一行
[[email protected] 2016]# cat num.txt 1 2 3 [[email protected] 2016]# cat newaa ming tian ni hao hello word [[email protected] 2016]# cat num.txt newaa 读取多个文件 1 2 3 ming tian ni hao hello word [[email protected] 2016]# paste num.txt newaa 1 ming tian ni hao 2 hello word 3 [[email protected] 2016]# -d 指定分隔符 [[email protected] 2016]# paste -d : num.txt newaa 1:ming tian ni hao 2:hello word 3: [[email protected] 2016]# -s 参数 每个文件占用一行 [[email protected] 2016]# paste -s num.txt newaa 1 2 3 ming tian ni hao hello word [[email protected] 2016]# [[email protected] liangli]# cat a.txt 1 2 3 4 5 6 7 8 9 10 [[email protected] liangli]# cat b.txt hello word wuhan shanghai [[email protected] liangli]# paste a.txt 1 2 3 4 5 6 7 8 9 10 [[email protected] liangli]# paste -s a.txt 1 2 3 4 5 6 7 8 9 10 [[email protected] liangli]# paste -s b.txt hello word wuhan shanghai [[email protected] liangli]#
3.9 sort
文本排序
-n 参数 按照数值排序
-r 参数 倒叙排序 从大到小排序
-u 参数 可以压缩重复行(可以压缩不相连的行)
-t 参数 指定分隔符
-k 参数 指定区域
[[email protected] 2016]# cat oldboy.txt 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.5 10.0.0.4 10.0.0.3 10.0.0.7 10.0.0.8 默认按照ASCII码排序 比较原则是从首字符向后 升序 从小到大 [[email protected] 2016]# sort oldboy.txt 10.0.0.3 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.5 10.0.0.7 10.0.0.8 [[email protected] 2016]# -n 参数 按照数值排序 -r 从大到小排序 [[email protected] 2016]# sort -r oldboy.txt 10.0.0.8 10.0.0.7 10.0.0.5 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.3 [[email protected] 2016]# -u 参数 可以压缩重复行(可以压缩不相连的行) [[email protected] 2016]# sort -u oldboy.txt 10.0.0.3 10.0.0.4 10.0.0.5 10.0.0.7 10.0.0.8 [[email protected] 2016]# 其实-u 就是uniq 我们也可以用uniq参数进行相应的实现(必须压缩相连行) [[email protected] 2016]# uniq oldboy.txt 10.0.0.4 10.0.0.5 10.0.0.4 10.0.0.3 10.0.0.7 10.0.0.8 [[email protected] 2016]# -rn 先按照数值排序 然后按照数值倒叙排序 [[email protected] 2016]# sort -rn oldboy.txt 10.0.0.8 10.0.0.7 10.0.0.5 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.3 [[email protected] 2016]# 默认按照第一列排序 [[email protected] 2016]# sort oldboy.txt 10.0.0.3 c 10.0.0.4 g 10.0.0.4 k 10.0.0.4 r 10.0.0.4 y 10.0.0.5 a 10.0.0.7 f 10.0.0.8 d [[email protected] 2016]# -t 指定分隔符 -k 指定区域 [[email protected] 2016]# sort -k oldboy.txt sort: 区块起始处的编号无效:在"oldboy.txt" 处的计数无效 [[email protected] 2016]# sort -k2 oldboy.txt 默认以空格作为分隔符的 k2就是第二列排序 10.0.0.5 a 10.0.0.3 c 10.0.0.8 d 10.0.0.7 f 10.0.0.4 g 10.0.0.4 k 10.0.0.4 r 10.0.0.4 y [[email protected] 2016]# 中间是tab键 [[email protected] 2016]# cat oldboy.txt 10.0.0.4 r 10.0.0.4 g 10.0.0.4 d 10.0.0.5 a 10.0.0.4 k 10.0.0.3 c 10.0.0.7 f 10.0.0.8 d [[email protected] 2016]# sort -k2 oldboy.txt 默认也可以以tab作为空格符 10.0.0.5 a 10.0.0.3 c 10.0.0.4 d 10.0.0.8 d 10.0.0.7 f 10.0.0.4 g 10.0.0.4 k 10.0.0.4 r [[email protected] 2016]# [[email protected] 2016]# sort -k2 oldboy.txt 10.0.0.3:c 10.0.0.4:d 10.0.0.4:g 10.0.0.4:k 10.0.0.4:r 10.0.0.5:a 10.0.0.7:f 10.0.0.8:d [[email protected] 2016]# sort -t: -k2 oldboy.txt 10.0.0.5:a 10.0.0.3:c 10.0.0.4:d 10.0.0.8:d 10.0.0.7:f 10.0.0.4:g 10.0.0.4:k 10.0.0.4:r [[email protected] 2016]#
3.10 join
按两个文件的相同字段合并
使用join合并文件的要求是2个文件必须是sort排序后的
[[email protected] liangli]# cat a.txt key1 25 now2 25 route3 24 [[email protected] liangli]# cat b.txt key1 nan route3 lvu now2 nan [[email protected] liangli]# join a.txt b.txt key1 25 nan join: 文件2 没有被正确排序 route3 24 lvu [[email protected] liangli]# sort a.txt >a.txtn [[email protected] liangli]# sort b.txt >b.txtn [[email protected] liangli]# join a.txtn b.txtn key1 25 nan now2 25 nan route3 24 lvu [[email protected] liangli]#
3.11 uniq
去除重复行
-c 参数 去除重复行 并计算每行出现的次数
[[email protected] liangli]# cat c.txt 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 [[email protected] liangli]# uniq c.txt 10.0.0.4 [[email protected] liangli]# uniq -c c.txt 参数-c显示相应行出现的次数 4 10.0.0.4 [[email protected] liangli]# [[email protected] liangli]# uniq c.txt uniq只能对相邻的重复行进行去重操作 10.0.0.4 10.0.0.3 10.0.0.4 10.0.0.6 10.0.0.4 10.0.0.5 10.0.0.4 [[email protected] liangli]# sort c.txt 10.0.0.3 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.4 10.0.0.5 10.0.0.6 [[email protected] liangli]# sort c.txt | uniq 10.0.0.3 10.0.0.4 10.0.0.5 10.0.0.6 [[email protected] liangli]# sort c.txt | uniq -c 1 1 10.0.0.3 6 10.0.0.4 1 10.0.0.5 1 10.0.0.6 [[email protected] liangli]#
3.12 wc
统计文件的行数,单词数,字节数
wc 生厕所显示
-l (lines) 总行数
-L 字符数 指的是精确的字符数目
[[email protected] liangli]# wc /etc/inittab 26 149 884 /etc/inittab [[email protected] liangli]# wc -l /etc/inittab l表示总行数 26 /etc/inittab [[email protected] liangli]# wc -w /etc/inittab w表示单词数 149 /etc/inittab [[email protected] liangli]# wc -c /etc/inittab c表示字节数 [[email protected] liangli]# wc -L /etc/inittab -L表示最长行的长度 [[email protected] liangli]# wc -m /etc/inittab -m表示字符数 884 /etc/inittab [[email protected] liangli]# 78 /etc/inittab [[email protected] liangli]# 884 /etc/inittab [[email protected] liangli]# [[email protected] ~]# cat -n /etc/services | tail -1 10774 iqobject 48619/udp # iqobject [[email protected] ~]# wc -l /etc/services 10774 /etc/services [[email protected] ~]# sshd服务有1,说明该服务是存活的 [[email protected] ~]# ps -ef | grep "/sshd" root 1637 1 0 20:27 ? 00:00:00 /usr/sbin/sshd root 2474 2414 0 22:48 pts/0 00:00:00 grep /sshd [[email protected] ~]# ps -ef | grep "/sshd" | grep -v grep root 1637 1 0 20:27 ? 00:00:00 /usr/sbin/sshd [[email protected] ~]# ps -ef | grep "/sshd" | grep -v grep | wc -l 1 [[email protected] ~]# [[email protected] ~]# netstat -lntup | grep sshd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1637/sshd tcp 0 0 :::22 :::* LISTEN 1637/sshd [[email protected] ~]# netstat -lntup | grep sshd | wc -l 2 [[email protected] ~]#
3.13 iconv
转换文件的编码格式
Linux系统是UTF-8的编码,而Win7系统是GB2312的编码,从英文的角度来讲,二者没有区别,但是Win编辑的中文字符到Linux系统中就会有乱码,需要先转码在处理
使用-f参数指定文件原来的编码为gb2312 使用-t参数指定将要转换的编码utf-8
[[email protected] ~]# iconv -f gb2312 -t utf-8 test.txt
3.14 dos2unix
把windows平台的格式转换成unix平台的格式
先安装 yum -y install dos2unix
windows换行符
linux换行符
dos2unix 后面接文件
3.15 diff
比较两个文件异同(可以比较目录内的文件不同)只能同时比较两个文件
diff默认显示格式有如下三种提示
a 是add增加的意思
c 是改变
d 删除
[[email protected] liangli]# cat a.txt 1 2 3 4 5 6 [[email protected] liangli]# cat b.txt 4 5 6 7 8 [[email protected] liangli]# diff a.txt b.txt 1,3d0 < 1 < 2 < 3 6a4,5 > 7 > 8 [[email protected] liangli]# d/a前面的数字是文本1的行号,字母后面的文本2的行号,其中<打头的行属于文件1,以>打头的行属于文件2 [[email protected] liangli]# diff -y a.txt b.txt 1 < 2 < 3 < 4 4 5 5 6 6 > 7 > 8 [[email protected] liangli]# [[email protected] liangli]# diff -y -W 30 a.txt b.txt -W参数指定宽度 1 < 2 < 3 < 4 4 5 5 6 6 > 7 > 8 [[email protected] liangli]# [[email protected] liangli]# diff -c a.txt b.txt -c参数可以上下文输出 *** a.txt 2018-09-30 17:56:06.289845188 +0800 --- b.txt 2018-09-30 17:56:50.296847597 +0800 *************** *** 1,6 **** - 1 - 2 - 3 4 5 6 --- 1,5 ---- 4 5 6 + 7 + 8 [[email protected] liangli]# [[email protected] liangli]# diff -u a.txt b.txt -u参数 使用统一格式输出 --- a.txt 2018-09-30 17:56:06.289845188 +0800 +++ b.txt 2018-09-30 17:56:50.296847597 +0800 @@ -1,6 +1,5 @@ -1 -2 -3 4 5 6 +7 +8 [[email protected] liangli]# 比较两个目录 [[email protected] liangli]# tree . ├── a │ ├── 1 │ ├── 1.txt │ ├── 2 │ ├── 2.txt │ ├── 3 │ └── 3.txt ├── a.txt ├── b │ ├── 2 │ ├── 2.txt │ ├── 3 │ ├── 3.txt │ ├── 4 │ └── 4.txt └── b.txt 8 directories, 8 files [[email protected] liangli]# diff a b Only in a: 1 Only in a: 1.txt Common subdirectories: a/2 and b/2 Common subdirectories: a/3 and b/3 Only in b: 4 Only in b: 4.txt [[email protected] liangli]#
3.16 vimdiff
可视化对比工具 后面可以接4个文件进行同时对比
[[email protected] liangli]# vimdiff a.txt b.txt 还有 2 个文件等待编辑 1 | ------------------------------------------ 2 | ------------------------------------------ 3 | ------------------------------------------ 4 | 4 5 | 5 6 | 6 -------------------------------------------| 7 -------------------------------------------| 8 ~ | ~ ~ | ~ ~ | ~ ~ | ~ ~ | ~ ~ | ~ ~ | ~ ~ | ~ ~ | ~ a.txt 1,1 全部 b.txt 1,1 全部 "b.txt" 5L, 10C 退出的话,需要执行2次vim的操作:q
3.17 rev
反向读取文件内容
[[email protected] ~]# echo 123456|rev 654321 [[email protected] liangli]# cat a.txt 1 2 3 4 5 6 7 8 9 10 [[email protected] liangli]# rev a.txt 01 9 8 7 6 5 4 3 2 1 [[email protected] liangli]#
3.18 tr
替换或删除字符
-d 参数 删除字符
-s 参数 将连续的字符压缩成一个
-c 参数 取反的意思
[[email protected] ~]# cat person.txt 101,oldboy,CEO 102,zhangyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO 011111111 0111111100 111 11111 1111111 [[email protected] ~]# tr ‘abc‘ ‘ABC‘ < person.txt 将所有的小写abc转换成大写ABC 注意 是一一对应的 101,oldBoy,CEO 102,zhAngyAo,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO 011111111 0111111100 111 11111 1111111 [[email protected] ~]# [[email protected] ~]# tr ‘[a-z]‘ ‘[A-Z]‘ < person.txt 101,OLDBOY,CEO 102,ZHANGYAO,CTO 103,ALEX,COO 104,YY,CFO 105,FEIXUE,CIO 011111111 0111111100 111 11111 1111111 [[email protected] ~]# [[email protected] ~]# tr ‘[0-9]‘ ‘[A-Z]‘ < person.txt 注意一一对应 BAB,oldboy,CEO BAC,zhangyao,CTO BAD,Alex,COO BAE,yy,CFO BAF,feixue,CIO ABBBBBBBB ABBBBBBBAA BBB BBBBB BBBBBBB [[email protected] ~]# -d 参数 删除的功能 [[email protected] ~]# cat person.txt 101,oldboy,CEO 102,zhangyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO 011111111 0111111100 111 11111 1111111 [[email protected] ~]# tr -d 0 < person.txt 11,oldboy,CEO 12,zhangyao,CTO 13,Alex,COO 14,yy,CFO 15,feixue,CIO 11111111 1111111 111 11111 1111111 [[email protected] ~]# 凡是在文件中出现的o l d b o y字符都会被删除掉,而不是只删除oldboy字符串 [[email protected] liangli]# cat a.txt oldboyoldbyonihaowoshiliang [[email protected] liangli]# tr -d ‘oldboy‘ < a.txt nihawshiiang [[email protected] liangli]# 也可以将换行符删除掉 [[email protected] ~]# tr -d ‘ ‘< person.txt 101,oldboy,CEO102,zhangyao,CTO103,Alex,COO104,yy,CFO105,feixue,CIO0111111110111111100111111111111111[[email protected] ~]# [[email protected] ~]# tr ‘ ‘ ‘=‘< person.txt 101,oldboy,CEO=102,zhangyao,CTO=103,Alex,COO=104,yy,CFO=105,feixue,CIO=011111111=0111111100=111=11111=1111111=[[email protected] ~]# -s参数将连续的字符压缩成一个 [[email protected] liangli]# echo ‘oooolllddbbboyyyyy‘ oooolllddbbboyyyyy [[email protected] liangli]# echo ‘oooolllddbbboyyyyy‘ | tr -s oldboy oldboy [[email protected] liangli]#
3.19 od
用于输出文件的八进制、十六进制或者其他格式编码的字节 如od /bin/ls
3.20 tee
多重定向 比如 一边把相应的结果输入到屏幕 一边把结果输入到保存的
文件中
-a 参数 追加的意思
[[email protected] ~]# ls 2016 key_2018-09-19.tar.gz new2_aaac test.txt a key_2018-09-20.tar.gz new3_00 text2018.txt anaconda-ks.cfg liangli new3_01 text2018.txt.back d065 liangli1 new3_02 text.txt data liangli123.txt new_aa wuhan20181.txt f043 liangli_2018-09-18.tar.gz new_ab wuhan20182.txt f044 liangli.tar.gz new_ac wuhan20183.txt f055 lihao oldboy wuhan20184.txt install.log lihao_2018-09-18.tar.gz person.txt xargs install.log.syslog md5.log test xiaomi2.txt key new2_aaaa test1.txt xiaomi3.txt key_2018-09-17.tar.gz new2_aaab test.hard xiaomi.txt [[email protected] ~]# ls | tee /tmp/tee.txt 2016 a anaconda-ks.cfg d065 data f043 f044 f055 install.log install.log.syslog key key_2018-09-17.tar.gz key_2018-09-19.tar.gz key_2018-09-20.tar.gz liangli liangli1 liangli123.txt liangli_2018-09-18.tar.gz liangli.tar.gz lihao lihao_2018-09-18.tar.gz md5.log new2_aaaa new2_aaab new2_aaac new3_00 new3_01 new3_02 new_aa new_ab new_ac oldboy person.txt test test1.txt test.hard test.txt text2018.txt text2018.txt.back text.txt wuhan20181.txt wuhan20182.txt wuhan20183.txt wuhan20184.txt xargs xiaomi2.txt xiaomi3.txt xiaomi.txt [[email protected] ~]# [[email protected] ~]# cat /tmp/tee.txt 2016 a anaconda-ks.cfg d065 data f043 f044 f055 install.log install.log.syslog key key_2018-09-17.tar.gz key_2018-09-19.tar.gz key_2018-09-20.tar.gz liangli liangli1 liangli123.txt liangli_2018-09-18.tar.gz liangli.tar.gz lihao lihao_2018-09-18.tar.gz md5.log new2_aaaa new2_aaab new2_aaac new3_00 new3_01 new3_02 new_aa new_ab new_ac oldboy person.txt test test1.txt test.hard test.txt text2018.txt text2018.txt.back text.txt wuhan20181.txt wuhan20182.txt wuhan20183.txt wuhan20184.txt xargs xiaomi2.txt xiaomi3.txt xiaomi.txt [[email protected] ~]# -a 参数 追加的意思 不加参数-a 覆盖/tmp/tee.txt文件内容 [[email protected] ~]# wc -l /tmp/tee.txt 48 /tmp/tee.txt [[email protected] ~]# ls | tee -a /tmp/tee.txt 2016 a anaconda-ks.cfg d065 data f043 f044 f055 install.log install.log.syslog key key_2018-09-17.tar.gz key_2018-09-19.tar.gz key_2018-09-20.tar.gz liangli liangli1 liangli123.txt liangli_2018-09-18.tar.gz liangli.tar.gz lihao lihao_2018-09-18.tar.gz md5.log new2_aaaa new2_aaab new2_aaac new3_00 new3_01 new3_02 new_aa new_ab new_ac oldboy person.txt test test1.txt test.hard test.txt text2018.txt text2018.txt.back text.txt wuhan20181.txt wuhan20182.txt wuhan20183.txt wuhan20184.txt xargs xiaomi2.txt xiaomi3.txt xiaomi.txt [[email protected] ~]# wc -l /tmp/tee.txt 96 /tmp/tee.txt [[email protected] ~]#
3.21 vi、vim
在Tech目录下输入vi oldboy.txt Visual Interface(可视界面) i 进入编辑状态 I am studying Linux 按esc :wq 输入命令cat oldboy.txt查看oldboy.txt里面的内容 vi相当于windows的记事本 简单
vim相当于复杂的编辑器 功能复杂,高亮 自动缩进(写shell/python脚本用) :q不想保存退出 :q! 强制退出
vim的三种模式
1、普通模式
用vim命令打开一个文件,默认的状态就是普通文件,在这个模式中,不能进行编辑输入操作,但可以按“上下左右”键来移动光标,也可以执行一些操作命令进行如删除、复制、粘贴等之类的工作
2、编辑模式
在普通模式下按i进入编辑模式,可以看到窗口左下角有插入的标记“INSERT”或“插入”
3、命令模式
在普通模式下,输入:或者/或者?时,光标会自动定位在那一行,在这个模式中,可以执行保存,退出,搜索,显示行号等相关操作
vim命令的重要参数选项及说明
普通模式:
跳至开头 按gg就行
跳至末尾 按G就行
行首 0(数字0)
行末 $
按: 输入set nu 开启行号
ngg 移动到第n行,如11gg
dd 删除当前一行
yy 复制 按p就是粘贴
u 撤销
以上是关于第三章 文件过滤及内容编辑处理命令的主要内容,如果未能解决你的问题,请参考以下文章