[英]Copying last n lines to a new file and then removing the n lines from original
所以我有一個文件,我想將最后3000行移動到另一個不同的文件,然后從原始文件創建一個沒有最后3000行的新文件。
我正在使用Mac,我使用的命令如下:
tail -n 3000 fer2017-testing-reduced.arff >> fer2017-training-reduced-3000-more-instances.arff; head -n -3000 fer2017-testing-reduced.arff > fer2017-testing-reduced-3000-less-instances.arff
但是當我運行它時,我收到錯誤:
head: illegal line count -- -3000
我不確定我哪里出錯了,或者這可能是一個mac問題?
如果允許其他工具,也許去sed
sed -n '3000,${p}' file > filenew # print lines 3000 to end to new file
sed -i '3000,${d}' file # Use inplace edit to delete lines 3000 to end from orig.
這里的優點是$
auto匹配最后一行。
閱讀更多信息,為什么POSIX head
和tail
不具有等效功能 , head
的POSIX版本不接受-n
選項的負整數。
從POSIX head
命令文檔中進行Qutoting,
-n number
每個輸入文件的第一個數字行應復制到標准輸出。 應用程序應確保number-argument參數為正十進制整數。
您最好將head -n -3000
更改為tail -n +3001
,從第3000
行開始到文件結尾。 或者使用GNU支持的head
命令,它在Mac上作為GNU coreutils的一部分提供。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.