2.3 档案内容查阅
- cat 由第一行开始显示档案内容。
- tac 从最后一行开始显示(可以看出tac是cat的倒着写。)
- nl 显示的时候,顺着输出行号!
- head 只看头几行
- tail 只看尾巴就行
- od 以二进制的方式读取档案内容!(二进制需要了解)
- 直接检视档案内容可以使用 cat/tac/nl 这几个命令!
-
cat (concatenate连续)
-n: 打印出行号,连同空白行也会有行号,与 -b 的选项不同;
-b: 列出行号,仅针对非空白行做行号显示,空白行不标行号!
例1:检阅/etc/issue 这个档案的内容
cat /etc/issue
Ubuntu 16.04.3 LTS \n \l
给上面的例子加印行号:
用 -n查看:
cat -n /etc/issue
1 Ubuntu 16.04.3 LTS \n \l
2
用-b查看:
cat -b /etc/issue
1 Ubuntu 16.04.3 LTS \n \l
- cat配合more/less 效果更好。
-
tac(反向列示)
tac 与 cat 是相反的两个例子。tac是由最后一行到第一行,cat由第一行到最后一行显示。
- nl(添加行号打印)
$ nl /etc/issue
1 Ubuntu 16.04.3 LTS \n \l
$ nl -b a /etc/issue
1 Ubuntu 16.04.3 LTS \n \l
2
$ nl -b a -n rz /etc/issue
000001 Ubuntu 16.04.3 LTS \n \l
000002
$ nl -b a -n rz -w3 /etc/issue
001 Ubuntu 16.04.3 LTS \n \l
002
$ nl -b a -n rz -w2 /etc/issue
01 Ubuntu 16.04.3 LTS \n \l
02
nl 可以将输出的档案内容自动的补上行号!其预设的结果与cat -n 有点不太一样 , nl 可以将行号做比较多的显示设计,包括位数与是否补齐 0 等等功能。
-
head(取出前面几行)
选项与参数:
1.-n :后面接数字,代表显示几行的意思。默认的情况下,显示前面十行:(空格也算是一行)
$ head /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.
# Be 8 bit clean.
set input-meta on
set output-meta on
# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.
若要显示前面20行,head -n 20 /etc/inputrc
;
$ head -n 20 /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.
# Be 8 bit clean.
set input-meta on
set output-meta on
# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.
# set convert-meta off
# try to enable the application keypad when it is called. Some systems
# need this to enable the arrow keys.
# set enable-keypad on
# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys
# do not bell on tab-completion
问题: 好像也没有20行?
2.后面100行如果不打印:head -n -100/etc/man.config
(注意-100)。
tail(取出后面几行):
选项与参数:
- -n:后面数字,代表显示几行的意思。
- -f: 表示持续侦测后面所接的 档名,要等到按下【ctrl】-c 才会结束tail的侦测。
- 与head相比较记忆:
tail -n +100 /etc/man.config
代表从改档案100行以后都会被列出来。 - 我的tail与head有同样的问题存在样。
- 需要显示 /etc/man.config 的11行到20行?
head -n 20 /etc/man.config l tail -n 10
需要用到管道命令,后面学习。
od:非纯文本文件:
选项或参数:
- -t: 后面可以接各种【类型(TYPE0】的输出,例如:
a :利用默认的字符来输出;(有点多,显示不完)
c :使用ASCⅡ字符来输出。