I have a text file which contains exactly thousand lines. I want to split the file into 1000 different files by writing each line to a separate text file. And i want the name of splitted files to be in a sequential order.

Example:

foo.txt file contains,

foo
bar
...
...
lastfoo

First Splitted file must contain only a single line foo and it's name would be bar1.txt. And the second file must contain only a single line bar and it's name would be bar2.txt likewise for the last file should contain a line lastfoo and the name of the last file would be bar1000.txt.

Command-line(One liner) way would be better rather than a script way.

  • "Command-line(One liner) way would be better rather than a script way.", meaning exclusively, or would a tiny script be acceptable as an alternative? – Jacob Vlijm May 25 '14 at 19:15

use this

split -l1 -a4 -d foo.txt bar

it creates 1000 file:

bar0000
bar0001
bar0002
bar0003
...

see man split for more.... About options:

-l, --lines=NUMBER
put NUMBER lines per output file
-a, --suffix-length=N
use suffixes of length N (default 2)
-d, --numeric-suffixes
use numeric suffixes instead of alphabetic
  • Would be great if you could offer an explanation. – jobin May 25 '14 at 11:20
  • for i in $(seq 1 1000); do mv barprintf "%04d" $i bar$i.txt will remove the zero padding. – S Prasanth May 26 '14 at 8:01

Since you mentioned command line would be better, I understood a script is not preferred, but not excluded as well.

In the script below, define sourcefile, destination directory and (output)filename in the top line.

In the line:

while currnumber <= len(lines) and currnumber <= 1000:

the last outputline is defined to be line 1000, but it can of course be set to any (max) line if you want to limit the output. Alternatively, you can remove the and currnumber <= 1000 part, to export all lines.

To use another (or no) extension, replace .txt, or remove +".txt"

The script:

#!/usr/bin/python3

source = "sourcefile"; destination = "/path/to"; outfilename = "outfilename"

with open(source) as lines:
    lines = lines.readlines()
currnumber = 1
while currnumber <= len(lines) and currnumber <= 1000:
    file = destination+"/"+outfilename+str(currnumber)+".txt"
    with open(file, "wt") as writefile:
            writefile.write(lines[currnumber-1])
    currnumber = currnumber+1

Paste it in an empty file, save it as export_lines.py and run it by the command:

python3 /path/to/script/export_lines.py

The result:

outfilename1.txt
outfilename2.txt

etc.
  • Still a single line :) :

    c=0; while read -r LINE || [[ -n $LINE ]]; do c=$(( $c+1 )); echo $LINE>bar$c.txt; done <foo.txt
    
  • Too long command, it's better with alias:

    alias s='c=0; while read -r LINE || [[ -n $LINE ]]; do c=$(( $c+1 )); echo $LINE>bar$c.txt; done <'
    

    Run as:

    s foo.txt
    

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.