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

Hello everyone, It has been awhile since I have used Perl, therefore I have lost some of my knowledge since I am not using it as often. I am trying to capture a line after skipping a variable of characters and whitespaces, my $find is at column 32 and I want to copy from there and there after for each line that matches my $find. I believe I as missing something or have something in the wrong place as the code works but I get zero results back. Thank you in advance.

use strict; my $find = '^(H0|HT)'; open (NEW, ">", "OUTPUT.txt" ) or die "could not open:$!"; open (FILE, "<", "INPUT.txt") or die "could not open:$!"; while (<FILE>) { print NEW if /^(.{32}$find)/; } close (FILE); close (NEW);
------------------- INPUT FILE: JANE, DOE HT000000000 123 50 70 80 JOHN, DOE JR H0000000000 123 50 70 80 WILLIAM, DOE HT000000000 123 50 70 80 ------------------- EXPECTED OUTPUT FILE: HT000000000 123 50 70 80 H0000000000 123 50 70 80 HT000000000 123 50 70 80

Replies are listed 'Best First'.
Re: Copy Line After Skipping Columns
by GotToBTru (Prior) on Nov 15, 2016 at 21:30 UTC

    The ^ in $find wants to start looking at the beginning of the string. Try it without that.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      I feel so silly right now.. hahahaha. That fixed it... Thank you so much. :)

Re: Copy Line After Skipping Columns -- oneliner
by Discipulus (Canon) on Nov 16, 2016 at 09:14 UTC
    Hello jlope043

    if you are refreshing your Perl skill, please note the modern form of opening files using lexical filehandle instead of bareword ones:

    open my $fh, '<', $filepath or die "could not open:$!";

    Also if you use the low precedence or parens are unneeded.

    If I understand your need your $find begins at 18 not 32:

    # warning windows doublequote perl -lanE "print $1 if /^.{17}(.*)/" datacolumn.txt HT000000000 123 50 70 80 H0000000000 123 50 70 80 HT000000000 123 50 70 80
    Or if you want to use @F faciliy provided by -a Perl switch you can print element using negative indexes (because you have JOHN, DOE JR ie variable lenght fields)
    perl -lanE "say join ' ',@F[-5,-4,-3,-2,-1]" datacolumn.txt HT000000000 123 50 70 80 H0000000000 123 50 70 80 HT000000000 123 50 70 80

    Finally if you want to refresh your Perl Modern Perl is very worth a read and is free!

    HtH

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.