juo has asked for the wisdom of the Perl Monks concerning the following question:

I have a very long ASCII file and I need to interrogate line by line but to be able to do that I need to be sure that the pattern I am looking for is in that line and not in between two lines. To do that I want to CR a piece of the pattern first. For example:
dasdfdafdsfadfdfaG57ewefdsfsdfsG 57dfadfasdfasdfsffdasG57dfsafdsf sG57dfafdafdsfadfafdfasdfasdfasf dafdfasdfasdfdsafasdfsdafasdfads dfadfasfdsdfadfasdfasfdadsfasfdd G57dsafdsfasdfadsfasdfadfadfadff dfdf
I want to create a new file that looks like this :
dasdfdafdsfadfdfa G57ewefdsfsdfs G57dfadfasdfasdfsffdas G57dfsafdsfs G57dfafdafdsfadfafdfasdfasdfasfd afdfasdfasdfdsafasdfsdafasdfadsd fadfasfdsdfadfasdfasfdadsfasfdd G57dsafdsfasdfadsfasdfadfadfadff dfdf
I looked everywhere but couldn't find how to do it.

Replies are listed 'Best First'.
(tye)Re: How to look for patterns in a file and create a CR
by tye (Sage) on Mar 21, 2001 at 00:01 UTC
    { local $_= <DATA>; while( defined $_ ) { chomp; s/(.)G57/$1\nG57/g; print $1 if s/(.*\n)//s; if( ! /G5?$/ ) { print $_,$/ if "" ne $_; $_= <DATA>; } else { my $next= <DATA>; last unless defined $next; $_ .= $next; } } print $_,$/; } __END__ dasdfdafdsfadfdfaG57ewefdsfsdfsG 57dfadfasdfasdfsffdasG57dfsafdsf sG57dfafdafdsfadfafdfasdfasdfasf dafdfasdfasdfdsafasdfsdafasdfads dfadfasfdsdfadfasdfasfdadsfasfdd G57dsafdsfasdfadsfasdfadfadfadff dfdf

    This produces:

    dasdfdafdsfadfdfa G57ewefdsfsdfs G57dfadfasdfasdfsffdas G57dfsafdsf s G57dfafdafdsfadfafdfasdfasdfasf dafdfasdfasdfdsafasdfsdafasdfads dfadfasfdsdfadfasdfasfdadsfasfdd G57dsafdsfasdfadsfasdfadfadfadff dfdf
    rather than what you asked for because it doesn't "join" lines unless it has to in order to match the string.

    Update: This node briefly showed incorrect code because I forgot to paste the tested code back in.

            - tye (but my friends call me "Tye")

      This didn't make it work completely. I have found the way to do it. First I remove all the returns and make one file of it. After that I build in again returns looking for a certain pattern. If somebody can make it <CR> shorter and knows how I can skip to make intermediate file and create straigt away the target file it is welcome. Pieter Here is the code :

      #!perl -w use strict; use FileHandle; my $file = "c:/perl/training/test/common.grb"; #source file my $file1 = "c:/perl/training/test/test5.txt"; #intermediate file, jus +t contain one long line my @users = readFile($file); for(@users) { chomp; s/\s+/ /g; } writeFile("c:/perl/training/test/test5.txt", @users); my @newUsers = readFile($file1); for (@newUsers) { s/(.)G57/$1\nG57/g; } writeFile("c:/perl/training/test/test6.txt", @newUsers);> #target fil +e, each line start with G57 sub readFile { my ($fileName) = @_; sysopen(F, $fileName, O_RDONLY) or die "\nCannot open #<CR> $file: $!\ +n"; my @fileContents = <F>; close(F); return @fileContents; } sub writeFile { my ($file, @contents) = @_; if(-e $file) { unlink $file or die "\nunable to remove $file: $!\n"; } sysopen(F, $file, O_RDWR | O_EXCL | O_CREAT, 0666) or die "\nCannot cr +eate $file: $!\n"; print F @contents or die "\nCannot write to $file: $!\n"; close(F) or die "\nCannot close filehandle - $file: $!\n"; }

      Edit 2001-03-21 by mirod: added code tags

Re: How to look for patterns in a file and create a CR
by Albannach (Monsignor) on Mar 20, 2001 at 19:38 UTC
    It's a bit hard to tell exactly what you want to do (please use <code> tags when you enter something that needs to retain its format), but here is a guess that might set you on your way. It slurps the entire file up at once (which may not work if it is really huge), then removes single newlines embedded in the pattern and adds a leading newline. Naturally you may want to generalize this to work for other patterns: perl -e "undef $/;$_=<>;s/G\n?5\n?7/\nG57/g;print;" yourfile.txt
      When I submit a question and I perform a preview all the lines or standing after each other and my returns dissepeared. With adding <code> do you mean that every time I want a return I should add at the end of my line #<#CR> ? Pieter
        Put code tags like this:
        <code>
        use strict; my $var = 0; ...
        </code>
        around your code.
        Its in the Site How To

        --
        my $chainsaw = 'Perl';