Opening a file in perl


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Opening a file in perl
# 1  
Old 07-22-2011
Opening a file in perl

Hi I need to open a file if a condition(for example a if a regular expression) is met. How do i do this ?

open (file) if (some regex).....
# 2  
Old 07-22-2011
Code:
if ($variable =~ /abc/)
{
#open file here
}
else
{ 
print "not matching"
}

# 3  
Old 07-22-2011
Thanks, I already had this in mind but i need help in the following input :
abcd diff
random data(any number of lines)
efgh diff
random data(any number of lines)
What I exactly need to do is when regex finds diff and I need to start printing from the next line till it reaches the next diff.So basically I am trying to open a file from the position where regex is matched and then sumhow get to print whatever is in the next lines. Could you please help me do this.
# 4  
Old 07-23-2011
Hammer & Screwdriver Files

Quote:
Originally Posted by manutd
Thanks, I already had this in mind but i need help in the following input :
abcd diff
random data(any number of lines)
efgh diff
random data(any number of lines)
What I exactly need to do is when regex finds diff and I need to start printing from the next line till it reaches the next diff.So basically I am trying to open a file from the position where regex is matched and then sumhow get to print whatever is in the next lines. Could you please help me do this.

Hi,

Try this one,


my $rc=0;
my $filename="";
if ( open(FILE,"<3.pl") )
{
while(<FILE>)
{
chomp($_);
if ( $_ =~/diff$/ )
{
$filename=(split(" ",$_))[0];
open(NFILE,">$filename");
}
print NFILE "$_\n";
}
}
else
{
print "Unable to open file\n";
}


Cheers,
RangaSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Opening a file in vi and automatically save and quit this file using shell script

Hi friends, In my shell script, I want to open a file using vi editor. After opening the file in vi, I want to save and quit this file automatically.... all through shell script. the code segment is: ------------------------------------------------------------ cd ~/netfpga/projects/scone/sw/... (2 Replies)
Discussion started by: sachinteotia
2 Replies

2. Shell Programming and Scripting

Xmllint - Xml-file problem --ods file not opening

Dear All, this is my first post on this Forum, glad to be here. I'm trying to fix an .ods file. Yes, I had a backup, but it's also corrupted. When opening the document I get this EM: read error format error discovered in the file in sub-document content.xml at 2,337040(row,col). So I... (3 Replies)
Discussion started by: jameslast
3 Replies

3. Shell Programming and Scripting

Printing a message in file without opening it in perl

Hello friends, i have a perl script as below ... for (0 ..$#values) { ##want to print some message here in Report.txt file print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt } I am able to get output of print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt in... (2 Replies)
Discussion started by: harpal singh
2 Replies

4. Shell Programming and Scripting

Opening File names with spaces inside it !- PERL

I developed a perl code..And the excerpt from it is given below... open(HANDLE,$cmp_path) ; #reading the xml file from the file path while($file_path = <HANDLE>) I have list of XML files to read from a folder. It has some spaces inside the name of the file...I used "\"... (2 Replies)
Discussion started by: gameboy87
2 Replies

5. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

6. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

7. Shell Programming and Scripting

Opening Files and checking contents in Perl

Hi All, I need some expert help in performing the following in Perl. I have a code below but it doesn;t seem to work. Can any expert give me some advice? Below are the requirements 1) Open numerous files assigned to an array @FILES. Note that the files are always named with the term... (7 Replies)
Discussion started by: Raynon
7 Replies

8. Shell Programming and Scripting

Perl: Opening a filehandle but not getting anything back from it

I have two perl functions defined, both run a set of shell commands on some somplied data and return hashs of the resulting parsed output from these shell commands. One works, one doesn't and I can't seem to see why. It's driving me insane :mad: The working one: sub getcellstatus { ... (8 Replies)
Discussion started by: Smiling Dragon
8 Replies

9. UNIX for Dummies Questions & Answers

opening a file

im trying to open a file in my bin directory and the farthest i can get is into the folder but not the actual file itself i use text edit as the editor. when i try to open the file it says it doesn't exist here is what i type cd bin open cmnds (dir. which it is under) open test no such... (4 Replies)
Discussion started by: hiei
4 Replies

10. Shell Programming and Scripting

Opening Perl

I have gone to /usr/bin/ and click on perl but notting happens.also notting happens when i click on c/c++ or any other program whats wrong ? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question