shell脚本---常用命令head

  • 用途

head - output the first part of files

Print the first 10 lines of each FILE to standard output.  With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.

  • 语法格式

head [OPTION]... [FILE]... 

-c, --bytes=[-]NUMprint the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file
-n, --lines=[-]NUMprint the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file
 -q, --quiet, --silentnever print headers giving file names
 -v, --verbosealways print headers giving file names
 -z, --zero-terminatedline delimiter is NUL, not newline
 --helpdisplay this help and exit
--versionoutput version information and exit


                   

  • 实例

1.  -c, --bytes=[-]NUM

     print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file 打印每个文件的前NUM个字节;以‘-’开头,打印除了每个文件的最后NUM个字节之外的所有字节。

renhl@verygood:~/usb$ echo ABCDEFGHIJABCDEFGHIJ > 2.txt
renhl@verygood:~/usb$ echo 01234567890123456789 > 1.txt
renhl@verygood:~/usb$ hexdump -C 1.txt #有21个字节
00000000  30 31 32 33 34 35 36 37  38 39 30 31 32 33 34 35  |0123456789012345|
00000010  36 37 38 39 0a                                    |6789.|
00000015
renhl@verygood:~/usb$ hexdump -C 2.txt  #有21个字节
00000000  41 42 43 44 45 46 47 48  49 4a 41 42 43 44 45 46  |ABCDEFGHIJABCDEF|
00000010  47 48 49 4a 0a                                    |GHIJ.|
00000015
renhl@verygood:~/usb$ head -c 3 1.txt 2.txt #打印每个文件的前3个字节
==> 1.txt <==
012
==> 2.txt <==
ABCrenhl@verygood:~/usb$ head -c -3 1.txt 2.txt #打印每个文件的除了后3个字节之外的所有字节,注意0x0a换行也算在最后3字节之内
==> 1.txt <==
012345678901234567
==> 2.txt <==
ABCDEFGHIJABCDEFGH

2.  -n, --lines=[-]NUM

print the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file 打印每个文件的前NUM;以‘-’开头,打印除了每个文件的最后NUM个之外的所有

renhl@verygood:~/usb$ seq 1 20 
renhl@verygood:~/usb$ for i in $(seq 65 85); do echo "$(printf "\x$(printf '%X\n' $i) $(($i-65))" )"; done > 2.txt
renhl@verygood:~/usb$ for i in $(seq 65 85); do printf "\x$(printf '%X\n' $i) %d\n" $(($i-65)); done > 2.txt

renhl@verygood:~/usb$ head -n 3 1.txt 2.txt #打印前3行
==> 1.txt <==
1
2
3

==> 2.txt <==
A 0
B 1
C 2
renhl@verygood:~/usb$ head -n -3 1.txt 2.txt #从开始到最后第3行
==> 1.txt <==
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

==> 2.txt <==
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
K 10
L 11
M 12
N 13
O 14
P 15
Q 16
R 17
renhl@verygood:~/usb$

 3. -q, --quiet, --silent

       never print headers giving file names 不打印带文件名的头部

renhl@verygood:~/usb$ head -n 3 1.txt 2.txt  #打印带文件名的头部
==> 1.txt <==
1
2
3

==> 2.txt <==
A 0
B 1
C 2
renhl@verygood:~/usb$ head -n 3 -q 1.txt 2.txt #-q参数不打印带文件名的头部
1
2
3
A 0
B 1
C 2
renhl@verygood:~/usb$

  4. -v, --verbose

     always print headers giving file names 打印带文件名的头部 

renhl@verygood:~/usb$ head -n 3 1.txt #不打印带文件名的头部
1
2
3
renhl@verygood:~/usb$ head -n 3 -v 1.txt #-v参数 打印带文件名的头部
==> 1.txt <==
1
2
3
renhl@verygood:~/usb$

5. -z, --zero-terminated

line delimiter is NUL, not newline 以NUL为行结尾

renhl@verygood:~/usb$ seq 1 5 | tr '\n' '\0' > 1.txt #作成用NUL间隔的1到5的数字
renhl@verygood:~/usb$ hexdump -C 1.txt
00000000  31 00 32 00 33 00 34 00  35 00                    |1.2.3.4.5.|
0000000a
renhl@verygood:~/usb$ head -n 3 1.txt #取得前3行,由于默认\n为分隔符,所以只有1行
12345renhl@verygood:~/usb$
renhl@verygood:~/usb$ head -n 3 -z 1.txt # 使用NUL为换行符,取得前3行,所以是123
123renhl@verygood:~/usb$

  • 22
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值