簡體   English   中英

將最后n行復制到新文件,然后從原始文件中刪除n行

[英]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問題?

並非所有版本的head支持負線計數。 macOS上安裝的默認設置沒有。

如果您安裝了coreutils (如果您安裝了Homebrew ,則可以執行此操作: brew install coreutils )您應該能夠使用ghead -n -3000

如果允許其他工具,也許去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 headtail不具有等效功能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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM