-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.

4
  • You mean you want get line from 10000 to 40000 and skip the rest? Commented Aug 13, 2014 at 15:38
  • @Gnouc Yes, i want to skip rest Commented Aug 13, 2014 at 15:39
  • @Aravind: See the link Ramesh gave. Commented Aug 13, 2014 at 15:40
  • What NIX platform are you using? Commented Aug 13, 2014 at 15:57

3 Answers 3

1

Use awk:

awk ' NR<=10000{ next} NR<=40000{print > "out2.txt"; next} ' input.txt 
2
  • That's a bit convoluted way to write awk 'NR > 10000 && NR <= 40000' input.txt > out2.txt Commented Aug 13, 2014 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! Commented Aug 13, 2014 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})

0

You can use sed:

 sed -n '10000,40000p' <infile 
1
  • Better sed '10000,$!d;40000q' or tail -n +10000 | head -n 30001 to stop reading after the 40000th line. Commented Aug 13, 2014 at 16:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.