想要改进这个问题? Update the question所以它是on-topic堆栈溢出。
关闭 9 年前。
我想从一个特定的行号拆分一个 400k 行长的日志文件。
对于这个问题,让我们将其设为任意数字 300k。
是否有允许我执行此操作的 linux 命令(在脚本中)?
我知道 split
让我可以按大小或行号将文件分成相等的部分,但这不是我想要的。我想要一个文件中的前 300k 和第二个文件中的最后 100k。
任何帮助将不胜感激。谢谢!
再想一想,这将更适合 super 用户或服务器故障站点。
最佳答案
file_name=test.log
# set first K lines:
K=1000
# line count (N):
N=$(wc -l < $file_name)
# length of the bottom file:
L=$(( $N - $K ))
# create the top of file:
head -n $K $file_name > top_$file_name
# create bottom of file:
tail -n $L $file_name > bottom_$file_name
另外,再想一想,拆分将适用于您的情况,因为第一个拆分大于第二个拆分。 split把输入的余额放到最后一个split中,所以
split -l 300000 文件名
将输出 300k 行的 xaa
和 100k 行的 xab
,用于 400k 行的输入。
https://stackoverflow.com/questions/3066948/