16

Is there a utility that split file by newline symbol? e.g if a file contains the following lines,

aa
bbb
cccc

If I want to split it to 3 files, the desired output would be:

aa, bbb And cccc (in 3 different files)

I already checked the split command, it only cut file by file sizes, not what I want.

If I don't wrote a utility myself, is there any standard tool to use?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
daisy
  • 53,527
  • 78
  • 236
  • 383

3 Answers3

20

Unless I'm missing something, split does split by line if you use -l switch:

   -l, --lines=NUMBER
          put NUMBER lines per output file

so

split -l 1 inputfile

should do what you want.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
4
awk '{print > $0".txt" }'  inputfile

would create one file per unique line in inputfile named after the content of those lines (with a .txt extension). But beware that when the limit of concurrent open files is reached, some awk implementations will fail.

Or

awk '{f = "output_file." NR; print $0 > f; close(f)}' inputfile

To have numbered output files.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96
3
A=0
while IFS= read -r LINE ; do
  printf '%s\n' "$LINE" > newfile$A
  (( A++ ))
done < "$INPUTFILE"
Uwe
  • 3,257
  • 17
  • 19