How to use perl to generate files with correct filenames?

Hi,

I'm trying to use perl to generate files based on sections in a large textfile.

This will create one file per section that starts with " ABC_":

perl -n -e '/^ABC_/ and open FH, ">output_".$n++; print FH;' largefile.txt

However, the output filenames will be on the form output_nn. This is not the way I would like it.

The text ABC_ that indicates the start of my block is first letters of a unique word (like ABC_asdf, ABC_qwer, ABC_fdsaqwe, etc). I would like this word to be my output file names, so that the files would be named ABC_asdf, ABC_qwer, ABC_fdsaqwe, etc.

How may I change my "script" to support this?

Thanks to anyone that might be willing to help me out here :slight_smile:

Try:

perl -n -e '/^ABC_(\w+)/ and open FH, ">ABC_$1"; print FH;' largefile.txt
1 Like

That solved my problem, Thanks a lot bartus11:b: