3

I have a very large file looks like this :

//abc/file1.js

some javascript code

//abc/file2.js

some javascript code

//abc/file3.js

some javascript code

Here I want to split this large file into pieces and store the pieces into file1.js,file2.js etc.

WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • @lhf, Could you give more detail or example ? Thanks ! – WoooHaaaa Jun 28 '13 at 03:47
  • See also http://stackoverflow.com/questions/8544197/splitting-a-file-in-linux-based-on-content and http://stackoverflow.com/questions/4323703/looking-for-correct-regular-expression-for-csplit. – lhf Jun 28 '13 at 04:14

3 Answers3

2

You can do this with awk. Print out each input line, but to a file name that changes whenever the input line indicates that a new file starts.

awk '
    /^\/\/abc\// { filename = $1; sub(/.*\//, "", filename); next; }
    filename { print >filename }
'

Remove the call to next if you want the header lines to be included, e.g. to have //abc/file1.js as the first line of file1.js. You may want to tweak the code that recognizes header lines depending on your requirements. Text prior to the first header line will not be printed anywhere; change filename { … } to 1 { … } if you want to print it to standard output.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
1

Try csplit -k -f file - '/^\/\//' '{1000}' < largefile.

Adjust 1000 to a suitable number. If there are n files in largefile, use n-2 instead of 1000.

If you're using GNU csplit, you can simply use * instead of 1000.

If there are many files in largefile, you'll need also to use -n 4 or some higher value.

lhf
  • 70,581
  • 9
  • 108
  • 149
-3

If you can edit the file, and you know exactly where you want to split, not by some byte offset, then just copy the new pieces to new files and save those new pieces and the existing files with the names you want. That is, using the editor itself.

Ayman
  • 11,265
  • 16
  • 66
  • 92
  • 1
    This file is very large, over 150,000 lines. – WoooHaaaa Jun 28 '13 at 04:45
  • That's not so large :-) I worked with files much larger with Vim. The only reason I'm saying this is that if the file is not just arbitrary, and you need to split at certain text, then an editor is the best option IMHO. – Ayman Jun 28 '13 at 05:00