Welcome to Perlmonks.

Is this what the actual code looks like?

#!/usr/bin/perl $string = 'test'; $inputs = 'test.txt'; open(TABLE, $inputs) or die "can't open $inputs.\n"; while ( $line = <IN> ) { if ( $_ =~ $string ) { print $_; } } close(IN);
( See Writeup Formatting Tips for info on getting your code into the node. )

If that's the case, then I see at least these problems at a glance:

You might try the below code (untested):

#!/usr/bin/perl use strict; use warnings; my $string = 'test'; my $inputs = 'test.txt'; # see perlfunc open( my $in, '<', $inputs) or die "can't open $inputs.\n"; while ( <$in> ) { # see perlsyn, perlop if ( $_ =~ m/^$string(.*)/ ) { # see perlre print $1; # see perlvar, perlre } } close( $in ); # see perlfunc
If, on the other hand, you want to just split on the first space instead of actually matching:
#!/usr/bin/perl use strict; use warnings; while ( <> ) { # takes filenames on the command line or STDIN if none print ( ( split )[1] ); }
perlvar, perlop, perlre, perlfunc are recommended reading. Also, if you're new to programming, consider Learning Perl. If you're a programmer new to Perl, consider Programming Perl.

BTW, Perl is a wonderful tool, but you might also want to 'man 1 cut' if you're using a Unixy OS.

Update: The code was missing a couple of $ sigils. Thanks to ikegami for pointing it out to me.


In reply to Re: Searching through a file by mr_mischief
in thread Searching through a file by marco.shaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.