0

I want to use linux command lines to split a file into several files which will have different lines. How can I make it?

E.g. Suppose a file with 1000 lines, how can I split first 600 lines into file1, and remaining 400 lines into file2? I know split can split a big file by same lines, but I don't know wheather I can still use it here.

I'll highly appreciated If anyone could help me. Thanks!!

1 Answer 1

1

For your example if you use

split FILE --lines=600

The last 400 lines will end up in the last fragment.

If you want to do arbitrary splits I'd suggest combining head and tail.

# e.g. get the 300 lines following line 250
tail  -n +250  FILE | head -n 300
Sign up to request clarification or add additional context in comments.

1 Comment

Repeatedly running head | tail on the same file is rather inefficient. A simple sed script only needs one pass. sed -e '1,12wonefile' -e '13,17wanother' -e '18,$wetc' writes the first 12 lines to one file, the next five to another, and the remainder to a third. (Some dialectal variation between platforms - see the local man page for what exactly the syntax looks like on your box.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.