Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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
share|improve this answer
    
Looks good - but does not work. In your form, complains about "expression for `>>' redirection has null string value", and if "file" is "changed" to "filename", outputs files of the form wrd.{file number}.{line number}.txt (quite a lot of them :) –  Rogach Feb 25 '12 at 5:15
    
@Rogach Sorry, I hadn't tested it, so I forgot awk doesn't do integer division. I've tested this one. –  Kevin Feb 25 '12 at 6:07

Such tasks are best managed with the shell. Use split and then write a simple loop to rename the files. E.g.

for file in wrd.*
do
    mv "$file" "$file.txt"
done

would rename your wrd.01, wrd.02, etc. files so they all have a .txt extension.

share|improve this answer
    
That's quite obvious, but it would break the conciseness of bash script. –  Rogach Feb 25 '12 at 5:08
    
The Unix philosophy is to provide you with a set of simple tools that you then combine to do a job. The "conciseness of the bash script" was not a stated requirement in your question. –  Kyle Jones Feb 25 '12 at 5:19
    
I agree, but there are different combinations of simple tools out there, and some do job better. I just hoped that there was a good combination that could do this job better that a 5-line long split with file renaming. –  Rogach Feb 25 '12 at 5:20
    
Just a personal obversation: When you actually get both versions to do the same thing with similar var names... awk 79 chars, split+mv 78 chars - - - - awk '{f="wrd." sprintf("%03d",int((NR-1)/10000)) ".txt"; print >> f}' inputfile - - - - split -dl10000 -a3 inputfile wrd.; for f in wrd.???; do mv "$f" "$f".txt; done –  Peter.O Feb 25 '12 at 10:01
3  
PS: the split+mv combo is more that 6 times faster than awk (approx 3s vs 18s) for a 10 million line input file (75 MB)... the text in each line was its own line-number... Thanks for re-stating the "obvious" :) –  Peter.O Feb 25 '12 at 10:36

This wasn't available back then but with more recent versions of split one can use:

--additional-suffix=SUFFIX
              append an additional SUFFIX to file names.

so adding

--additional-suffix=.txt

to the command means the resulting files will automatically have .txt extension.

split -dl 10000 --additional-suffix=.txt words wrd
share|improve this answer

The --additional-suffix option of split was added in GNU coreutils 8.16.

split now accepts the --additional-suffix option, to append an additional static suffix to output file names.

http://savannah.gnu.org/forum/forum.php?forum_id=7170

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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