-1

I have a file in which more than 50,000 lines are there. How can I split the file into two or more based on the selected lines?

Suppose I want to split a file from line 10,000 to line 40,000.

Kevdog777
  • 3,194
  • 18
  • 43
  • 64
Aravind
  • 1,559
  • 9
  • 31
  • 44

3 Answers3

1

Use awk:

awk ' NR<=10000{ next}
NR<=40000{print > "out2.txt"; next} ' input.txt
garethTheRed
  • 33,289
  • 4
  • 92
  • 101
  • That's a bit convoluted way to write `awk 'NR > 10000 && NR <= 40000' input.txt > out2.txt` – Stéphane Chazelas Aug 13 '14 at 16:07
  • @StéphaneChazelas I have to confess - I thought the OP wanted three files originally. One for 0-10000, another for 10000-40000 and another for 40000 upwards. I quickly edited when I realised I'd miss-read the post by deleting the offending `print` statements. I should have edited properly or deleted the post really! – garethTheRed Aug 13 '14 at 17:21
1

If you want lines 1 to 9999 in one file, 10000 to 40000 in a second and the rest in a 3rd, you can use:

csplit -f file.out file.in 10000 40001

(will store in file.out0{0,1,2})

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
0

You can use sed:

 sed -n '10000,40000p' <infile
mdpc
  • 6,736
  • 3
  • 32
  • 46