CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
writing all the strings between two particular substrings

 



giridharreddy9
Novice

Oct 27, 2009, 9:56 PM

Post #1 of 9 (815 views)
writing all the strings between two particular substrings Can't Post

Hello , I am new to perl and I am already facing a few challenges. Any help would be appreciated. To properly explain my problem, I am actually pasting a sample data that I am working on.

Code
 input CK, n3065gat, n3066gat, n3067gat, n3068gat, n3069gat, n3070gat, 
+n3100gat, test_si, test_se;
output n3104gat, n3105gat, n3106gat, n3107gat, n3108gat, n3109gat, n
+3110gat;


What I need to achieve is to print all the words between the substrings "input" and "output" excluding both of them into another file. I have managed to achieve a part of the task. I am able to print from CK i.e after "input" to the end of that line i.e n3070gat. I am not able to print the lines below it.

Code
open(DATA, "s5378_scan.v") or die "error: $!"; 
while (<DATA>)
{
if ($_ =~ /input/)
{
$length_line = length ($_);
$x= $length_line - 7;
$portion = substr($_, 7, $x);
}
my $str = $portion;
my $find = ",";
my $replace = ";";
$find = quotemeta $find;
$str =~ s/$find/$replace/g;
open (MYFILE, '>data.txt');
print MYFILE "$str\n";
close (MYFILE);
} # End while
close(DATA)

Please note that I am replacing all the "comma's" with ";" at the same time while writing to the file. Also the substring input always starts at an offset of 7. I would appreciate if anyone could suggest any modifications/additions to the code.

--Giridhar


savo
User

Oct 28, 2009, 3:52 AM

Post #2 of 9 (811 views)
Re: [giridharreddy9] writing all the strings between two particular substrings [In reply to] Can't Post

How big is your input file? If its not to big you could read the whole file into memory then do the match.

If thats not an option could you give me an example of the file, does it just contain the data no description etc at the top?


giridharreddy9
Novice

Oct 28, 2009, 5:58 AM

Post #3 of 9 (805 views)
Re: [savo] writing all the strings between two particular substrings [In reply to] Can't Post

The biggest input file I have is of size 1.5 MB. I am attaching a smaller input file for your reference.

I appreciate your help.
Attachments: s5378_scan.v (195 KB)


FishMonger
Veteran / Moderator

Oct 28, 2009, 6:43 AM

Post #4 of 9 (801 views)
Re: [giridharreddy9] writing all the strings between two particular substrings [In reply to] Can't Post

DATA is a Perl built-in filehandle. It is best to use it as it's intended rather than using it to open a user specified file.

Don't use bareword filehandles. It's better to use a lexical var for the filehandle and the 3 arg form of open.

You're opening/reopening 'data.txt' for each and every line in s5378_scan.v. That open call should be done prior to the while loop.

Are there multiple input...output sections in the data file? The sample you posted only has 1.

Here's a reworked version that assumes that there is only i input...output section. If there are more than 1, then use 'next' instead of 'last'.


Code
#!/usr/bin/perl 

use strict;
use warnings;

open my $data, '<', 's5378_scan.v' or die "failed to open 's5378_scan.v' $!";
open my $myfile, '>', 'data.txt') or die "failed to open 'data.txt' $!";

while ( <$data> ) {
if ( /input/ .. /output/) {
last if /^\s*output/;
s/^\s*input //;
tr/,/;/;
print $myfile;
}
}

close $data;
close $myfile;



savo
User

Oct 28, 2009, 6:57 AM

Post #5 of 9 (799 views)
Re: [giridharreddy9] writing all the strings between two particular substrings [In reply to] Can't Post

Whilst i was working out how to do this fish has given you the corect way to do it but here is mine anyway.

it will read the whole file into a string because they are only small files it should be ok then match everything between input and output you could then do as you wanted with that.


Code
my $string; 

{
local $/ = undef;
open( DATA, "s5378_scan.v" ) or die "error: $!";
$string = <DATA>;
close DATA;
}

my $match;
if ( $string =~ /input(.*)output/sm ) {
print "$1\n";
}



giridharreddy9
Novice

Oct 28, 2009, 10:46 AM

Post #6 of 9 (793 views)
Re: [FishMonger] writing all the strings between two particular substrings [In reply to] Can't Post

Yes ,there is only one input and output in the file. When I tried to run your script, it prints this (not in the output file):

Code
GLOB(0x182af1c)GLOB(0x182af1c)GLOB(0x182af1c)GLOB(0x182af1c)GLOB(0x182af1c)GLOB( 
0x182af1c)


I am not sure if I am doing anything wrong.


FishMonger
Veteran / Moderator

Oct 28, 2009, 10:58 AM

Post #7 of 9 (791 views)
Re: [giridharreddy9] writing all the strings between two particular substrings [In reply to] Can't Post

Sorry, my error.

change:
print $myfile;

to:
print $myfile $_;


giridharreddy9
Novice

Oct 28, 2009, 11:02 AM

Post #8 of 9 (788 views)
Re: [FishMonger] writing all the strings between two particular substrings [In reply to] Can't Post

It worked.
Thank you a lot.


giridharreddy9
Novice

Oct 28, 2009, 11:03 AM

Post #9 of 9 (786 views)
Re: [savo] writing all the strings between two particular substrings [In reply to] Can't Post

Thanks a lot savo.

I appreciate your help.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives