1

I'm trying to split text file into files of 1024 lines, so I ran split with the -d switch:

split -d -l 300 ./list.lst

I get some weird names: they start with x and the file names jump from x89 to x9000. I want the files to be named like that:

1.lst
2.lst
3.lst
...

Thanks.

1

At least with the GNU Coreutils version of split, you can do it as follows:

split -l300 --numeric-suffixes=1 --suffix-length=1 --additional-suffix=".lst"  file ""

Note the use of "" to specify an empty prefix (the xa part of the default filename) and the use of --numeric-suffixes in place of -d (which always starts from 0).

Note also that this assumes that the file to be split contains no more than 9 x 300 lines - otherwise split will complain that output file suffixes exhausted

Ex.

$ split -l300 --numeric-suffixes=1 --suffix-length=1 --additional-suffix=".lst" --verbose file ""
creating file '1.lst'
creating file '2.lst'
creating file '3.lst'
creating file '4.lst'
  • With --suffix-length=1 it is only possible to create 9 parts (1-9). The user is creating more than 89 parts (because he reports filenames as x9000). So, length should be at least 3 (or more). – Isaac Jan 6 at 5:52
0

Try this:

 split --additional-suffix='.lst' -da3 -l 1024 list.lst ''

The reason for the jump to 9000 (for more than 89 parts) is that the default numeric length is 2.

Change with the -a option. To make sure the file numbering stay numeric (and is monotonic (always increase)) use a length that is longer than the maximum value that the possible number of splits.

 $ split -a 3 -d -l1024 ./list.lst

 $ ls
 list.lst  x009  x019  x029  x039  x049  x059  x069  x079  x089  x099  x109  x119
 x000      x010  x020  x030  x040  x050  x060  x070  x080  x090  x100  x110  x120
 x001      x011  x021  x031  x041  x051  x061  x071  x081  x091  x101  x111  x121
 x002      x012  x022  x032  x042  x052  x062  x072  x082  x092  x102  x112  x122
 x003      x013  x023  x033  x043  x053  x063  x073  x083  x093  x103  x113  x123
 x004      x014  x024  x034  x044  x054  x064  x074  x084  x094  x104  x114  x124
 x005      x015  x025  x035  x045  x055  x065  x075  x085  x095  x105  x115  x125
 x006      x016  x026  x036  x046  x056  x066  x076  x086  x096  x106  x116  x126
 x007      x017  x027  x037  x047  x057  x067  x077  x087  x097  x107  x117  x127
 x008      x018  x028  x038  x048  x058  x068  x078  x088  x098  x108  x118

To change the x, just change the PREFFIX (default x) (after the file name):

 split -a 3 -d -l 1024 list.lst ''

Which will name the files 000. To get a trailing .lst, add the --additional-suffix='.lst' option, in short, use this:

 split --additional-suffix='.lst' -da3 -l 1024 list.lst ''

If you need the start numeric value to be 001, use this:

 split --additional-suffix='.lst' --numeric-suffixes=1 -a 3 -l 1024 list.lst ''

But no, there is no way to get the numeric values like 1,2,3,...,21,..,107 without renaming.

The renaming could resolve all issues in two steps with a simple loop:

 split -l 1024 -d -a 8 list.lst
 for f in x[0-9]*; do
     mv "$f" "$((10#${f#x}+1)).lst"
 done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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