3

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 mouse and doing ctrl+c obviously is not a viable solution here.

How should I do this?

phs
  • 10,687
  • 4
  • 58
  • 84
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

4 Answers4

18
head -n 532541 big-file > first-bit
phs
  • 10,687
  • 4
  • 58
  • 84
4

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

Community
  • 1
  • 1
event_jr
  • 17,467
  • 4
  • 47
  • 62
2

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1
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

Kent
  • 189,393
  • 32
  • 233
  • 301