1、文件查看查看工具
Linuxzhong存在着很多配置文件以及脚本,那Linux中也自带了查看文本的工具:
cat 命令
简介: cat - concatenate files and print on the standard output
格式: cat [OPTION]... [FILE]...
选项: -E: 显示行结束符$ -n: 对显示出的每一行进行编号 -A:显示所有控制符 -b:非空行编号 -s:压缩连续的空行成一行
实例1: [root@centos6 ~]# cat -A /etc/fstab #显示纯文本文件的行结束符 $ #$ # /etc/fstab$ # Created by anaconda on Sat Aug 6 17:35:06 2016$ #$ # Accessible filesystems, by reference, are maintained under '/dev/disk'$ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$ #$ UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1$ UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2$ UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2$ UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0$ tmpfs /dev/shm tmpfs defaults 0 0$ devpts /dev/pts devpts gid=5,mode=620 0 0$ sysfs /sys sysfs defaults 0 0$ proc /proc proc defaults 0 0$
实例2: [root@centos6 ~]# cat -n /etc/fstab #对每行进行编号,对文本内容不做处理 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Sat Aug 6 17:35:06 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1 10 UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2 11 UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2 12 UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0 13 tmpfs /dev/shm tmpfs defaults 0 0 14 devpts /dev/pts devpts gid=5,mode=620 0 0 15 sysfs /sys sysfs defaults 0 0 16 proc /proc proc defaults 0 0
实例3: [root@centos6 ~]# cat -n /etc/fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Sat Aug 6 17:35:06 2016 5 # 6 # Accessible filesystems, by reference, are maintained under '/dev/disk' 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1 10 UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2 11 UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2 12 UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0 13 tmpfs /dev/shm tmpfs defaults 0 0 14 devpts /dev/pts devpts gid=5,mode=620 0 0 15 sysfs /sys sysfs defaults 0 0 16 proc /proc proc defaults 0 0 [root@centos6 ~]# cat -b /etc/fstab #对每行进行编号,除了空行不显示 1 # 2 # /etc/fstab 3 # Created by anaconda on Sat Aug 6 17:35:06 2016 4 # 5 # Accessible filesystems, by reference, are maintained under '/dev/disk' 6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 7 # 8 UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1 9 UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2 10 UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2 11 UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0 12 tmpfs /dev/shm tmpfs defaults 0 0 13 devpts /dev/pts devpts gid=5,mode=620 0 0 14 sysfs /sys sysfs defaults 0 0 15 proc /proc proc defaults 0 0
实例4: [root@centos6 ~]# cat cat.txt a b c d e f g [root@centos6 ~]# cat -s cat.txt #压缩连续的空行成一行 a b c d e f g [root@centos6 ~]#
实例5: tac命令是显示cat的逆序 [root@centos6 ~]# tac /etc/fstab proc /proc proc defaults 0 0 sysfs /sys sysfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 tmpfs /dev/shm tmpfs defaults 0 0 UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0 UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2 UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2 UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1 # # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # Accessible filesystems, by reference, are maintained under '/dev/disk' # # Created by anaconda on Sat Aug 6 17:35:06 2016 # /etc/fstab #
more 命令
简介: more - file perusal filter for crt viewing
格式: more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]
选项: -d: 显示翻页及退出提示
实例1: [root@centos6 ~]# more /etc/rc.d/init.d/functions #按百分比显示文件内容,可以用-d翻页 # -*-Shell-script-*- # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # TEXTDOMAIN=initscripts # Make sure umask is sane umask 022 # Set up a default search path. PATH="/sbin:/usr/sbin:/bin:/usr/bin" export PATH # Get a sane screen width [ -z "${COLUMNS:-}" ] && COLUMNS=80 [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="$(/sbin/consoletype)" if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" -a -z "${LANGSH_SOURCED:-}" ] ; then . /etc/profile.d/lang.sh 2>/dev/null # avoid propagating LANGSH_SOURCED any further unset LANGSH_SOURCED fi # Read in our configuration if [ -z "${BOOTUP:-}" ]; then if [ -f /etc/sysconfig/init ]; then --More--(2%)
less 命令
简介: less - opposite of more
实例1: [root@centos6 ~]# less /etc/rc.d/init.d/functions #显示该文件的内容(Tips:man手册实际用的是less命令查看手册) # -*-Shell-script-*- # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # TEXTDOMAIN=initscripts # Make sure umask is sane umask 022 # Set up a default search path. PATH="/sbin:/usr/sbin:/bin:/usr/bin" export PATH # Get a sane screen width [ -z "${COLUMNS:-}" ] && COLUMNS=80 [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="$(/sbin/consoletype)" if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" -a -z "${LANGSH_SOURCED:-}" ] ; then . /etc/profile.d/lang.sh 2>/dev/null # avoid propagating LANGSH_SOURCED any further unset LANGSH_SOURCED fi # Read in our configuration if [ -z "${BOOTUP:-}" ]; then if [ -f /etc/sysconfig/init ]; then /etc/rc.d/init.d/functions
2、显示文本前或后行内容
tail 命令
简介: tail - output the last part of files
格式: tail [OPTION]... [FILE]...
选项: -c #: 指定获取前#字节 -n #: 指定获取前#行 -#:指定行数
实例1: [root@centos6 ~]# head -n 5 /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
tail 命令
简介: tail - output the last part of files
格式: tail [OPTION]... [FILE]...
选项: -c #: 指定获取后#字节 -n #: 指定获取后#行 -#:指定行数 -f: 跟踪显示文件新追加的内容,常用日志监控
实例1: [root@centos6 ~]# tail -n 5 /etc/passwd gdm:x:42:42::/var/lib/gdm:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin nginx:x:500:500::/home/nginx:/bin/bash
3、按列抽取文本cut和合并文件paste
cut 命令
简介: cut - remove sections from each line of files
格式: cut OPTION... [FILE]...
选项: -d DELIMITER: 指明分隔符,默认tab -f FILEDS: #: 第#个字段 #,#[,#]:离散的多个字段,例如1,3,6 #-#:连续的多个字段, 例如1-6 混合使用:1-3,7 -c按字符切割 --output-delimiter=STRING指定输出分隔符
实例1: [root@centos6 ~]# cat /etc/passwd | cut -d: -f7 #已冒号为分隔符,显示文件"/etc/passwd"文件的第七列 /bin/bash /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/sync /sbin/shutdown /sbin/halt /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /sbin/nologin /bin/bash
paste 命令
简介: paste - merge lines of files
格式: paste [OPTION]... [FILE]...
选项: -d 分隔符:指定分隔符,默认用TAB -s : 所有行合成一行显示
实例1: [root@centos6 ~]# paste -s /etc/issue /etc/fstab #将两个文件内容合成一行显示 CentOS release 6.8 (Final) Kernel \r on an \m # # /etc/fstab # Created by anaconda on Sat Aug 6 17:35:06 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=9b5660be-73ed-4e5d-aff7-65e4841286f6 / ext4 defaults 1 1 UUID=e276612a-5263-4d01-a54d-a46a230b4a63 /boot ext4 defaults 1 2 UUID=12860f16-5f41-4741-a056-d4f5b3d43d7f /testdir ext4 defaults 1 2 UUID=5532054c-ccae-4bc5-aee2-00cf66edbcc8 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc
4、分析文本的工具
文本数据统计:
wc 命令
简介: wc - print newline, word, and byte counts for each file
格式: wc [OPTION]... [FILE]... wc [OPTION]... --files0-from=F
选项: -l:来只计数行数 -w:来只计数单词总数 -c:来只计数字节总数 -m:来只计数字符总数
实例1: [root@centos6 ~]# cat /etc/passwd |wc -l #统计文件"/etc/passwd"行数为34 34 [root@centos6 ~]# cat /etc/passwd |wc -w #统计文件"/etc/passwd"单词为53 53 [root@centos6 ~]# cat /etc/passwd |wc -c #统计文件"/etc/passwd"单词为1615 1615
文本排序:
sort 命令
简介: sort - sort lines of text files
格式: sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F
选项: -r:执行反方向(由上至下)整理 -n:执行按数字大小整理 -f:忽略(fold)字符串中的字符大小写 -u:(独特,unique)删除输出中的重复行 -t c:使用c做为字段界定符 -k X:按照使用c字符分隔的X列来整理能够使用多次
实例1: [root@centos6 ~]# tail /etc/passwd|sort -t: -k3 -n #以冒号为分隔符,并以第三列数字大小排序,将文件"/etc/passwd"最后十行排序。 ntp:x:38:38::/etc/ntp:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin saslauth:x:498:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin nginx:x:500:500::/home/nginx:/bin/bash
文本内容去重:
uniq 命令
简介: uniq - report or omit repeated lines
格式: uniq [OPTION]... [INPUT [OUTPUT]]
选项: -c: 显示每行重复出现的次数; -d: 仅显示重复过的行; -u: 仅显示不曾重复的行; Tips:连续且完全相同方为重复;常和sort 命令一起配合使用:
文本内容比较:
diff 命令
简介: diff - compare files line by line
格式: diff [OPTION]... FILES
实例1: [root@centos6 ~]# echo "how are you" > f11 [root@centos6 ~]# echo "how old are you" > f22 [root@centos6 ~]# diff f11 f22 -u > f33 [root@centos6 ~]# rm -f f22 [root@centos6 ~]# patch -b f11 f33 patching file f11 [root@centos6 ~]# cat f11.orig how are you [root@centos6 ~]# patch -b f11 f33 patching file f11 Reversed (or previously applied) patch detected! Assume -R? [n] y [root@centos6 ~]# cat f11.orig how old are you
原创文章,作者:Aleen,如若转载,请注明出处:http://www.178linux.com/30484