How to use perl to generate files with correct filenames?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use perl to generate files with correct filenames?
# 1  
Old 07-11-2012
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_":
Code:
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 Smilie
# 2  
Old 07-11-2012
Try:
Code:
perl -n -e '/^ABC_(\w+)/ and open FH, ">ABC_$1"; print FH;' largefile.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-11-2012
Quote:
Originally Posted by bartus11
Try:
Code:
perl -n -e '/^ABC_(\w+)/ and open FH, ">ABC_$1"; print FH;' largefile.txt

That solved my problem, Thanks a lot bartus11Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Generate filenames in a loop

Hi all, I want to generate output files in a loop, run the same command on the same input file 1000 times and output in files with a new name each time, maybe a number appended to it. The output will be different each time as I`m sampling randomly from the input file. I want to do the... (3 Replies)
Discussion started by: newbie83
3 Replies

3. Shell Programming and Scripting

How to get the correct hash value in perl?

Hi, I have 2 dummy strings below. I am creating a hash. @str = qw(karnataka,tamilnadu,bihar,mumbai); @str1 = qw(bangalore,chennai,patna,panaji); %hash; for($i=0;$i<=$#str;$i++) { push @{ $hash{ $str } }, $str1; } foreach $key (keys %hash) { print "\n KEY: $key --- @{$hash{$key}}... (5 Replies)
Discussion started by: vanitham
5 Replies

4. Shell Programming and Scripting

Unable to get the correct sort order in perl.

Hi, I have created the hash. %hash; @arr1 = qw(Dealnum AdminStatus adminReason effFrom effTo); @arr2 = qw(121212121 YES 1992-06-19T05:14:27 ); @hash{@arr1}=@arr2; foreach(sort keys %hash){ print "$_ ---- $hash{$_}\n"; } The output i got like this: C:\strawberry\perl\bin>perl... (1 Reply)
Discussion started by: vanitham
1 Replies

5. Shell Programming and Scripting

Perl: windows filenames escape characters

I have a perl find program that will find all files of window application stored on unix disks. Ofcourse these files contain all the weird characters windows allows, but on *nix pukes out all kinds of unwanted effects when processing these. Is their a utility that will escape all these... (3 Replies)
Discussion started by: karelb
3 Replies

6. Shell Programming and Scripting

Perl - grep issue in filenames with wildcards

Hi I have 2 directories t1 and t2 with some files in it. I have to see whether the files present in t1 is also there in t2 or not. Currently, both the directories contain the same files as shown below: $ABC.TXT def.txt Now, when I run the below script, it tells def.txt is found,... (5 Replies)
Discussion started by: guruprasadpr
5 Replies

7. Shell Programming and Scripting

Get Correct IP address in perl/cgi?

Hi, How to get correct IP address of a system? I tried using this but i didn't get exact output. Here is the code: $host = $ENV{'REMOTE_HOST'}; print "<br> host ip address ---> $host <br>"; $addr = $ENV{'REMOTE_ADDR'}; print "<br> host address ----- > $addr <br>"; But i am... (4 Replies)
Discussion started by: vanitham
4 Replies

8. Shell Programming and Scripting

perl script to list filenames that do not contain given string

Hi, I need to write a perl script that should do a search recursively in all the '*.txt' files for the string "ALL -Tcb" and should print only the file names that do not contain this string. (21 Replies)
Discussion started by: royalibrahim
21 Replies

9. Shell Programming and Scripting

generate random number in perl

Could any one tell how can I generate random number from (0, 100..200) in perl? Thanks! (2 Replies)
Discussion started by: zx1106
2 Replies

10. Shell Programming and Scripting

Correct Syntax For Calling Shell Script in Perl Module

Problem: I have a shell script that will be called by a Perl module that will connect to a db and delete rows. The Perl module will be called by CRON. I am using a Perl module to call a shell script because I need to get the db connection from Perl. Here is the Perl pseudocode: ... (4 Replies)
Discussion started by: mh53j_fe
4 Replies
Login or Register to Ask a Question