开发者

Extracting many leading lines out of a huge text file

I have a huge text file. I want to extract the first 532541 lines of it and store them in another file. Selecting the text with the 开发者_C百科mouse and doing ctrl+c obviously is not a viable solution here.

How should I do this?


head -n 532541 big-file > first-bit


In Emacs M-<C-SPCC-u532541C-nM-xwrite-region. If you do this often, then write a keyboard macro to do these steps.

Alternatively, you can define a function that inserts the current buffer's file-name like so. Then, M-!head -n 531541 F3> first-bit


head is the right tool for this job. With awk or sed, it's more efficient to quit processing the file after the target line, so the useless rest of the file does not have to be read.

sed '532541q' big > small
awk '{print} NR==532541 {exit}' big > small


awk 'NR<=532541' big > small
awk '{if(NR<=532541) print; else exit}' big > small  #if the file is really huge
sed -n '1,532541p' big > small
sed '1,532541!d' big > small
sed '532542,$d' big > small

:) have fun

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜