Sponsored Content
Top Forums Shell Programming and Scripting Perl, open multiple files with wildcards Post 302428995 by japaneseguitars on Friday 11th of June 2010 12:28:33 PM
Old 06-11-2010
Perl, open multiple files with wildcards

I have a question regarding Perl scripting.

If I want to say open files that all look like this and assign them to a filehandle and then assign the filehandle to a variable, how do I do this?

The file names are

strand1.fa.gz.tmp
strand2.fa.gz.tmp
strand3.fa.gz.tmp
strand4.fa.gz.tmp
...
...
strand19.fa.gz.tmp

I want to assign them to file handles similar to their name
strand1.fa.gz.tmp.fh
strand2.fa.gz.tmp.fh
...
...
etc

then I want to set variables so its

$strand1=<strand1.fa.gz.tmp.fh>
...
...

So far I have this

Code:
#! /usr/bin/perl

@files=glob("*.tmp");

foreach $data (@files) {
        open($data.fh, "<$data") || die "failed $data\n";
}

I don't know if using $data.fh is right or not. Should I push the filehandles into an array (is that possible)?

I want to do this so I don't have to open the file 19 times by writing 19 lines and then writing an additional 19 lines to assign them to a variable.

Thanks.

Last edited by pludi; 06-11-2010 at 03:35 PM.. Reason: code tags, please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using wildcards in this perl command

Hi there, is it possible to use wild cards in this statement ssh $remote_server 'perl -pi -e "s,EXP_SERIAL_19b8be67=\"\",EXP_SERIAL_`hostid`=\"UNKNOWN\"," /var/myfile' This command works fine but the bit in bold (the 8 character hostid) will not always be 19b8be67 so I was hoping I could... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

2. Shell Programming and Scripting

Grepping using multiple wildcards

Is there anyway you can grep using multiple wildcards? When I run the below line the results return fine; grep 12345 /usr/local/production/soccermatchplus/distributor/clients/*/out/fixtures.xml | awk -F/ '{print $8}' However ideally, I need it to grep for; grep 12345... (3 Replies)
Discussion started by: JayC89
3 Replies

3. Shell Programming and Scripting

Perl script to search and extract using wildcards.

Good evening All, I have a perl script to pull out all occurrences of a files beginning with xx and ending in .p. I will then loop through all 1K files in a directory. I can grep for xx*.p files but it gives me the entire line. I wish to output to a single colum with only the hits found. ... (3 Replies)
Discussion started by: CammyD
3 Replies

4. Shell Programming and Scripting

perl script on multiple files

I have a script that runs on one file (at a time). like this: $> perl myscript.pl filename > output How can I run it on >6000 files and have the output sent out into slightly modified file name $> perl myscript 6000files> output6000files.new extension Thanks in anticipation (4 Replies)
Discussion started by: aritakum
4 Replies

5. Shell Programming and Scripting

rename multiple files with wildcards

Hi All I am having hundred over file in the below pattern. AA050101.INI BB090101.INI . . ZX980101.INI Need to rename these files with an extension .bak AA050101.INI.bak BB090101.INI.bak . . ZX980101.INI.bak (5 Replies)
Discussion started by: karthikn7974
5 Replies

6. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

7. 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

8. Shell Programming and Scripting

Perl - work with open files or write to @lists first?

I am dealing will many thousand fairy small files. I need to search them for various matches and depending on what I find, may need to search some files again for additional matches. Generally speaking, is it better to write a txt file to an @array/@list and then work with it (multiple... (1 Reply)
Discussion started by: OldGaf
1 Replies

9. Shell Programming and Scripting

open files with multiple indexes

Hi, I want to work with multiple files which all contain 2 numbers. I tried to make a nested for loop but for some reason it doesn't recognize the $j as a number. The output is cannot open file `175-T-pvalue.xls'. How do I make sure that it takes the numbers from the inner loop as $j? ... (4 Replies)
Discussion started by: linseyr
4 Replies

10. Shell Programming and Scripting

Grep multiple patterns that contain wildcards

job_count=`grep -e "The job called .* has finished | The job called .* is running" logfile.txt | wc -l` Any idea how to count those 2 patterns so i have a total count of the finished and running jobs from the log file? If i do either of the patterns its works okay but adding them together... (8 Replies)
Discussion started by: finn
8 Replies
Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHUsereContributed Perl DocPerl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles(3)

NAME
Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles - Write "open my $fh, q{<}, $filename;" instead of "open FH, q{<}, $filename;". AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
Using bareword symbols to refer to file handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file handle. You can mitigate some of that risk by "local"izing the symbol first, but that's pretty ugly. Since Perl 5.6, you can use an undefined scalar variable as a lexical reference to an anonymous filehandle. Alternatively, see the IO::Handle or IO::File or FileHandle modules for an object-oriented approach. open FH, '<', $some_file; #not ok open my $fh, '<', $some_file; #ok my $fh = IO::File->new($some_file); #ok There are three exceptions: STDIN, STDOUT and STDERR. These three standard filehandles are always package variables. CONFIGURATION
This Policy is not configurable except for the standard options. SEE ALSO
IO::Handle IO::File AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2014-06-09 Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles(3)
All times are GMT -4. The time now is 01:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy