Shell – Split a File by Line and Control Resulting File Extensions

filenamesshellsplit

There is a standard command for file splitting – split.

For example, if I want to split a words file in several chunks of 10000 lines, I can use:

split -dl 10000 words wrd

and it would generate several files of the form wrd.01, wrd.02 and so on.

But I want to have a specific extension for those files – for example, I want to get wtd.01.txt, wrd.02.txt files.

Is there a way to do it?

Best Answer

Not with split, but you can easily rename them afterwards, or you can do it in awk:

awk '{filename = "wrd." int((NR-1)/10000) ".txt"; print >> filename}' inputfile
Related Question