Loading ...
Sorry, an error occurred while loading the content.

How to find word in file

Expand Messages
  • iful@themail.com
    Dear all, Now I am writing some perl script, and I have some problem to find/searching one/more word/field in file. What should I do? Thank alot. Iful
    Message 1 of 10 , Dec 11, 2000
    • 0 Attachment
      Dear all,

      Now I am writing some perl script, and I have some problem to find/searching one/more word/field in file. What should I do?

      Thank alot.

      Iful
      __________________________________________________________________
      Make A Buck Or Two @ TheMail.com - Free Internet Email
      Sign-up today at http://www.themail.com/ref.htm?ref=1819017
    • Gerrit P. Haase
      ... On unix/linux i would say, use grep . Try
      Message 2 of 10 , Dec 12, 2000
      • 0 Attachment
        <Den 11 Dec 2000, 21:06 Uhr, hat iful@... geschrieben:>
        < [PBML] How to find word in file >

        > Now I am writing some perl script, and I have some problem to
        > find/searching one/more word/field in file. What should I do?
        >
        > Thank alot.

        On unix/linux i would say, 'use grep'.
        Try http://www.gnu.org/software/grep

        On windows? Maybe you can get grep fr windows somewhere, i think
        there are some ports, don't ask me where.

        Another option on windows is to use an editor. Textpad, e.g., there
        you open ten or hundred files and search the string in all files, you
        could enter regular expressions to search for, you could substitute
        strings, in a lot f files parrallel.

        Another option is to use perl itself, thats what it is for:

        Call: perl perlgrep.pl searchfile.txt

        #!usr/local/bin/perl
        # perlgrep.pl
        # gph gerrit@...

        $string = 'whatyousearch'; # the string you search
        $anotherstring = 'whatelseyousearch'; # another string
        $linecount = 1; # to get the linenumber

        while (<>) { # as long as input comes
        chomp; # cut off newline
        m! ($string) # one searchstring
        | # or
        ($anotherstring) # another searchstring
        !x; # x allows comments in regex's
        # saved with the braces
        # () in $1 and $2
        if ($1) {
        print $ARGV,": "; # $ARGV contains the current
        # input-filename
        print "line $linecount: found $1\n"; # print matches
        }
        if ($2) {
        print $ARGV,": ";
        print "line $linecount: found $2\n";
        }
        $linecount++; # increment $linecount
        }

        gph

        --
        Gerrit Peter Haase
        haase@...
      • Gerrit P. Haase
        ... [...] ... next; ... I made a mistake, $1 and $2 need
        Message 3 of 10 , Dec 12, 2000
        • 0 Attachment
          <Den 12 Dec 2000, 11:37 Uhr, hat Gerrit P. Haase geschrieben:>
          < Re: [PBML] How to find word in file >

          > <Den 11 Dec 2000, 21:06 Uhr, hat iful@... geschrieben:>
          > < [PBML] How to find word in file >
          >
          > > Now I am writing some perl script, and I have some problem to
          > > find/searching one/more word/field in file. What should I do?
          [...]
          > Another option is to use perl itself, thats what it is for:
          >
          > Call: perl perlgrep.pl searchfile.txt
          >
          > #!usr/local/bin/perl
          > # perlgrep.pl
          > # gph gerrit@...
          >
          > $string = 'whatyousearch'; # the string you search
          > $anotherstring = 'whatelseyousearch'; # another string
          > $linecount = 1; # to get the linenumber
          >
          > while (<>) { # as long as input comes
          > chomp; # cut off newline
          > m! ($string) # one searchstring
          > | # or
          > ($anotherstring) # another searchstring
          > !x; # x allows comments in regex's
          > # saved with the braces
          > # () in $1 and $2
          > if ($1) {
          > print $ARGV,": "; # $ARGV contains the current
          > # input-filename
          > print "line $linecount: found $1\n"; # print matches
          > }
          > if ($2) {
          > print $ARGV,": ";
          > print "line $linecount: found $2\n";
          > }
          > $linecount++; # increment $linecount
          next;
          > }

          I made a mistake, $1 and $2 need to be initialized in some cases, so
          better add 'next' at end of while-loop.

          gph

          --
          Gerrit Peter Haase
          haase@...
        • Gerrit P. Haase
          ... Hmmm, it is not that easy, if you re a beginner
          Message 4 of 10 , Dec 12, 2000
          • 0 Attachment
            <Den 12 Dec 2000, 12:39 Uhr, hat Gerrit P. Haase geschrieben:>
            < Re: [PBML] How to find word in file >

            > <Den 12 Dec 2000, 11:37 Uhr, hat Gerrit P. Haase geschrieben:>
            > < Re: [PBML] How to find word in file >
            >
            > > <Den 11 Dec 2000, 21:06 Uhr, hat iful@... geschrieben:>
            > > < [PBML] How to find word in file >
            > >
            > > > Now I am writing some perl script, and I have some problem to
            > > > find/searching one/more word/field in file. What should I do?
            > [...]
            > > Another option is to use perl itself, thats what it is for:
            > >
            > > Call: perl perlgrep.pl searchfile.txt

            Hmmm, it is not that easy, if you're a beginner (like me, too).
            I made it handier:

            Call the script with searchstring as first argument and file(s) to
            search in as second argument:
            $ perl perlgrep.pl searchstring filestosearchin

            #!usr/local/bin/perl
            # perlgrep.pl
            # gph gerrit@...

            $string = shift;
            $linecount = 1;

            while (<>) {
            chomp;
            if (m!($string)!) {
            print $ARGV,": ";
            print "line $linecount: found $1\n";
            }
            $linecount++;
            next;
            }


            gph

            --
            Gerrit Peter Haase
            haase@...
          • Charles Clarkson
            ... find/searching one/more word/field in file. What should I do? ... # search string - each word separated by | my $search = is|my|party ; # you can supply
            Message 5 of 10 , Dec 12, 2000
            • 0 Attachment
              --- In perl-beginner@egroups.com, iful@t... wrote:
              > Dear all,
              >
              > Now I am writing some perl script, and I have some problem to
              find/searching one/more word/field in file. What should I do?
              >
              > Thank alot.
              >
              > Iful

              # search string - each word separated by |

              my $search = 'is|my|party';

              # you can supply @ARGV here or
              # put your file names on the command line

              @ARGV = "quotes.txt";

              # <> will grab each file in @ARGV and loop
              # through each line.
              while (<>) {

              # restart the loop if $search not found
              next unless /($search)/i;

              # show the match
              # $1 holds the last search.
              # $ARGV holds the current file name.
              # $. holds the current line number.
              # $_ holds the current line.
              print "Found \'$1\' in $ARGV on line $.\n";
              }
            • Andrew Johnson
              Gerrit wrote: [snip] ! $string = shift; ! $linecount = 1; ! ! while ( ) { ! chomp; ! if (m!($string)!) { ! print $ARGV, : ; ! print
              Message 6 of 10 , Dec 12, 2000
              • 0 Attachment
                Gerrit wrote:
                [snip]

                ! $string = shift;
                ! $linecount = 1;
                !
                ! while (<>) {
                ! chomp;
                ! if (m!($string)!) {
                ! print $ARGV,": ";
                ! print "line $linecount: found $1\n";
                ! }
                ! $linecount++;
                ! next;
                ! }

                We don't need the chomp() in this case, and we don't need to
                count lines ourselves because the $. special variable already
                does that for us. Also, using 'next' at the end of a loop is
                unnecessary (where else would the loop go at that point?).

                If we want to accept a pattern and one or more filenames on
                the command line, we should check that at least 2 arguments
                have been given. Also, if we are providing line numbers and
                there is more than one file, we need to reset line numbering
                by closing the ARGV handle at each eof:

                #!/usr/bin/perl -w
                use strict;
                @ARGV >= 2 or die "You must supply at least two arguments\n";
                my $search = shift;
                while (<>) {
                print "$ARGV: line $.: $_" if /$search/io;
                close ARGV if eof;
                }

                The above expects 2 or more arguments, uses the first one as the
                search pattern, searches case-insensitively, and compiles the pattern
                only once.

                Lastly, if you are looking for "words" then you might want to add \b
                anchors to your pattern as in:

                /\b$search\b/

                That is, if your search term is 'foo' and you want it to trigger
                on the string 'foo bar', but not on 'food bar' then add the word
                boundary anchors around your search term.

                For further info see:

                perldoc perlre (info on regular expressions)
                perldoc -f eof (info on the eof function from perlfunc)
                perldoc perlvar (info on Perl's special variables)

                regards,
                andrew

                --
                Andrew L. Johnson http://members.home.net/andrew-johnson/
              • Gerrit P. Haase
                ... At first i made it without the next, but i got
                Message 7 of 10 , Dec 12, 2000
                • 0 Attachment
                  <12 Dec 2000, 9:43 Uhr wars, als Andrew Johnson folgendes schrub:>
                  < Re: [PBML] How to find word in file >

                  > Gerrit wrote:
                  > [snip]
                  >
                  > ! $string = shift;
                  > ! $linecount = 1;
                  > !
                  > ! while (<>) {
                  > ! chomp;
                  > ! if (m!($string)!) {
                  > ! print $ARGV,": ";
                  > ! print "line $linecount: found $1\n";
                  > ! }
                  > ! $linecount++;
                  > ! next;
                  > ! }
                  >
                  > We don't need the chomp() in this case, and we don't need to
                  > count lines ourselves because the $. special variable already
                  > does that for us. Also, using 'next' at the end of a loop is
                  > unnecessary (where else would the loop go at that point?).

                  At first i made it without the next, but i got wrong output.
                  I wonder myself, looked over with the debugger and couldn't see whats
                  the problem is, then i added the next and it works fine, maybe there is
                  a bug in ActivePerl?

                  > If we want to accept a pattern and one or more filenames on
                  > the command line, we should check that at least 2 arguments
                  > have been given. Also, if we are providing line numbers and
                  > there is more than one file, we need to reset line numbering
                  > by closing the ARGV handle at each eof:
                  >
                  > #!/usr/bin/perl -w
                  > use strict;
                  > @ARGV >= 2 or die "You must supply at least two arguments\n";
                  > my $search = shift;
                  > while (<>) {
                  > print "$ARGV: line $.: $_" if /$search/io;
                  > close ARGV if eof;
                  > }
                  >
                  > The above expects 2 or more arguments, uses the first one as the
                  > search pattern, searches case-insensitively, and compiles the pattern
                  > only once.
                  >
                  > Lastly, if you are looking for "words" then you might want to add \b
                  > anchors to your pattern as in:
                  >
                  > /\b$search\b/
                  >
                  > That is, if your search term is 'foo' and you want it to trigger
                  > on the string 'foo bar', but not on 'food bar' then add the word
                  > boundary anchors around your search term.
                  >
                  > For further info see:
                  >
                  > perldoc perlre (info on regular expressions)
                  > perldoc -f eof (info on the eof function from perlfunc)
                  > perldoc perlvar (info on Perl's special variables)
                  >
                  > regards,
                  > andrew

                  Thanks a lot for the tips, i've learned s.th. new again:))
                  --
                  Gerrit Peter Haase
                • iful@themail.com
                  ... Iful wrote: OK, thank for the explaination how to find word in file. It work. But how to print line content? For example: if one line/field content perl1
                  Message 8 of 10 , Dec 12, 2000
                  • 0 Attachment
                    >> Call: perl perlgrep.pl searchfile.txt
                    >
                    >>
                    >
                    >> #!usr/local/bin/perl
                    >
                    >> # perlgrep.pl
                    >
                    >> # gph gerrit@...
                    >
                    >>
                    >
                    >> $string = 'whatyousearch'; # the string you search
                    >
                    >> $anotherstring = 'whatelseyousearch'; # another string
                    >
                    >> $linecount = 1; # to get the linenumber
                    >
                    >>
                    >
                    >> while (<>) { # as long as input comes
                    >
                    >> chomp; # cut off newline
                    >
                    >> m! ($string) # one searchstring
                    >
                    >> | # or
                    >
                    >> ($anotherstring) # another searchstring
                    >
                    >> !x; # x allows comments in regex's
                    >
                    >> # saved with the braces
                    >
                    >> # () in $1 and $2
                    >
                    >> if ($1) {
                    >
                    >> print $ARGV,": "; # $ARGV contains the current
                    >
                    >> # input-filename
                    >
                    Iful wrote:

                    OK, thank for the explaination how to find word in file. It work.
                    But how to print line content?
                    For example: if one line/field content "perl1 perl2 perl3 perl4 perl5"
                    After found line, and we want to print the content of line it again, how?

                    Thank.

                    >> print "line $linecount: found $1\n"; # print matches
                    >
                    >> }
                    >
                    >> if ($2) {
                    >
                    >> print $ARGV,": ";
                    >
                    >> print "line $linecount: found $2\n";
                    >
                    >> }
                    >
                    >> $linecount++; # increment $linecount
                    >
                    > next;
                    >
                    >> }

                    __________________________________________________________________
                    Make A Buck Or Two @ TheMail.com - Free Internet Email
                    Sign-up today at http://www.themail.com/ref.htm?ref=1819017
                  • Gerrit P. Haase
                    ... At first, i found gnu-grep for NT, which is much
                    Message 9 of 10 , Dec 13, 2000
                    • 0 Attachment
                      <Den 13 Dec 2000, 1:01 Uhr, hat iful@... geschrieben:>
                      < Re: [PBML] How to find word in file >

                      > Iful wrote:
                      >
                      > OK, thank for the explaination how to find word in file. It work.
                      > But how to print line content?
                      > For example: if one line/field content "perl1 perl2 perl3 perl4 perl5"
                      > After found line, and we want to print the content of line it again, how?
                      >
                      > Thank.

                      At first, i found gnu-grep for NT, which is much handier in some
                      cases, here:
                      http://www.tardis.ed.ac.uk/~skx/win/Free11.html#grep

                      Your question, there were two good answers on the list:

                      1. Charles Clarkson wrote:

                      # search string - each word separated by |

                      my $search = 'is|my|party';

                      # you can supply @ARGV here or
                      # put your file names on the command line

                      @ARGV = "quotes.txt";

                      # <> will grab each file in @ARGV and loop
                      # through each line.
                      while (<>) {

                      # restart the loop if $search not found
                      next unless /($search)/i;

                      # show the match
                      # $1 holds the last search.
                      # $ARGV holds the current file name.
                      # $. holds the current line number.
                      # $_ holds the current line.
                      print "Found \'$1\' in $ARGV on line $.\n";
                      }

                      2. Andrew Johnson wrote:
                      Gerrit wrote:
                      [snip]

                      ! $string = shift;
                      ! $linecount = 1;
                      !
                      ! while (<>) {
                      ! chomp;
                      ! if (m!($string)!) {
                      ! print $ARGV,": ";
                      ! print "line $linecount: found $1\n";
                      ! }
                      ! $linecount++;
                      ! next;
                      ! }

                      We don't need the chomp() in this case, and we don't need to
                      count lines ourselves because the $. special variable already
                      does that for us. Also, using 'next' at the end of a loop is
                      unnecessary (where else would the loop go at that point?).

                      If we want to accept a pattern and one or more filenames on
                      the command line, we should check that at least 2 arguments
                      have been given. Also, if we are providing line numbers and
                      there is more than one file, we need to reset line numbering
                      by closing the ARGV handle at each eof:

                      #!/usr/bin/perl -w
                      use strict;
                      @ARGV >= 2 or die "You must supply at least two arguments\n";
                      my $search = shift;
                      while (<>) {
                      print "$ARGV: line $.: $_" if /$search/io;
                      close ARGV if eof;
                      }

                      The above expects 2 or more arguments, uses the first one as the
                      search pattern, searches case-insensitively, and compiles the pattern
                      only once.

                      Lastly, if you are looking for "words" then you might want to add \b
                      anchors to your pattern as in:

                      /\b$search\b/

                      That is, if your search term is 'foo' and you want it to trigger
                      on the string 'foo bar', but not on 'food bar' then add the word
                      boundary anchors around your search term.

                      For further info see:

                      perldoc perlre (info on regular expressions)
                      perldoc -f eof (info on the eof function from perlfunc)
                      perldoc perlvar (info on Perl's special variables)

                      regards,
                      andrew

                      --
                      Andrew L. Johnson http://members.home.net/andrew-johnson/



                      --
                      Gerrit Peter Haase
                      haase@...
                    • Sheldon Smith
                      The while ( ) reads the line into variable $_. The match command assigns the matching string to variable $1.
                      Message 10 of 10 , Dec 13, 2000
                      • 0 Attachment
                        The "while (<>)" reads the line into variable $_.
                        The match command assigns the matching string to variable $1.

                        > -----Original Message-----
                        > From: iful@... [mailto:iful@...]
                        > Sent: Wednesday, December 13, 2000 12:02 AM
                        > To: perl-beginner@egroups.com
                        > Subject: Re: [PBML] How to find word in file
                        >
                        >
                        > >> while (<>) { # as long as input comes
                        > >> m! ($string) # one searchstring
                        > >> | # or
                        > >> ($anotherstring) # another searchstring
                        > >> !x; # x allows
                        > >> if ($1) {
                        > Iful wrote:
                        >
                        > OK, thank for the explaination how to find word in file. It work.
                        > But how to print line content?
                        > For example: if one line/field content "perl1 perl2 perl3 perl4 perl5"
                        > After found line, and we want to print the content of line it
                        > again, how?
                        >
                        > Thank.
                      Your message has been successfully submitted and would be delivered to recipients shortly.