Shell实用文件操作

一些较为高级的文件操作(诸如统计行数,打乱内容)都可以利用shell命令快速的完成。这里记录自己用过的shell命令。

统计文件行数

1
wc -l filename

随机打乱文件内部的行

1
shuf input_file -o output_file

或者

1
cat input_file | shuf > output_file

Mac OS没有shuf命令,可以brew install coreutils,然后用gshuf代替

读取文件的头/尾若干行

1
cat filename | head -n 1000 # 读取头1000行

head替换为tail即读取最后1000行