Tell me more ×
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.

How can I split a file into multiple files based on pattern using perl?

Ex: input to be a .txt file, name of the output file stored in another .txt file

share|improve this question
2  
Can you give a bit more explanation as to what the input file is like, what pattern you want to match and what output files will be created? –  Drav Sloan Aug 24 at 12:31
2  
You need to give a little more information about what you want to accomplish for us to help you well. Also, asking for code here without showing that you even tried is poor form, although someone might give you free code anyway. –  msw Aug 24 at 12:32

closed as unclear what you're asking by msw, Mat, rahmu, Anthon, Chris Down Aug 24 at 13:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

Here, to open a new file on each occurrence of the pattern /8/:

$ seq 30 | perl -pe 'BEGIN{open STDOUT, ">", "file" . ++$n}
                     open STDOUT, ">", "file" . ++$n if /8/'
$ ls
file1  file2  file3  file4
$ paste *
1       8       18      28
2       9       19      29
3       10      20      30
4       11      21
5       12      22
6       13      23
7       14      24
        15      25
        16      26
        17      27
share|improve this answer
 
I think the question was more like using $.%4 to spread the lines in separate files. So line 1, 5, 9, … in file1, line 2, 6, 10, … in file2 and so on. Of course, just a guess. –  manatwork Aug 24 at 12:57

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