grep program for unix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lduggi
    New Member
    • Jan 2008
    • 2

    grep program for unix

    I want write a program to grep a file in unix and open a file with third party tool and redirect o/p to text file:
    Example:
    1.grep a file in a machine
    2.open a file with third party tools and redirect o/p to text file:
    if core dump occur in a machine and open with tool like gdb in hp or aix and o/p redirect to a text file.

    can any one help me through the it plz...
    thank you....


    ~rao
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    I'm sure someone can help, where are you stuck?

    Comment

    • t3chn0n3rd
      New Member
      • Dec 2007
      • 19

      #3
      just for starters

      grep (some statement) > redirect file

      I guess i need more info

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by lduggi
        I want write a program to grep a file in unix and open a file with third party tool and redirect o/p to text file:
        Example:
        1.grep a file in a machine
        2.open a file with third party tools and redirect o/p to text file:
        if core dump occur in a machine and open with tool like gdb in hp or aix and o/p redirect to a text file.

        can any one help me through the it plz...
        thank you....


        ~rao
        I agree with Kevin. Please post your code and we will try and assist you.

        Just out of curiosity, instead of third party software, why not just use Perl's abilities?

        Regards,

        Jeff

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by t3chn0n3rd
          just for starters

          grep (some statement) > redirect file

          I guess i need more info

          Let me just say that if:

          #1. you are using Unix
          #2. your script is named grep

          then this is a very bad practice as there is already a grep command in the system. You should name it something like 'newgrep' or something, to where it does not conflict with the system command(s).

          Regards,

          Jeff

          Comment

          • lduggi
            New Member
            • Jan 2008
            • 2

            #6
            grep is not a program name, i would like to grep a file called as core in unix and redirect o/p to a text file and this script should run continouly to catch core file and keep o/p in a text file:

            [code=perl]
            $file = "core" ;
            if($file = core)
            {
            system("pstack core >test.txt"); ---pstack is tool in solaris to read core dump.
            print( "core file found in a system");
            }
            else
            {
            print("core file not found in a system");
            }
            [/code]

            or is any effient way to write same program
            Last edited by numberwhun; Jan 6 '08, 01:05 PM. Reason: add code tags

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by lduggi
              grep is not a program name, i would like to grep a file called as core in unix and redirect o/p to a text file and this script should run continouly to catch core file and keep o/p in a text file:

              [code=perl]
              $file = "core" ;
              if($file = core)
              {

              print( "core file found in a system");
              }
              else
              {
              print("core file not found in a system");
              }
              [/code]

              or is any effient way to write same program
              Well, first, code tags are required to surround any code you place into your posts in the forums. Please make sure that the next time you post code that you use them. You can read more about using code tags here.

              Now, looking at your code I do see a few things that I would like to point out:

              1. By setting $file to have the value "core", it will always test as true. What you should be doing is obtaining a directory listing from where ever the core dumps are put and then scanning the listing for the file name "core" You can take the directory listing using back tics and store it into an array as such:

              [code=perl]
              my @listing = `ls <directory_path >`;
              [/code]

              Each element in the array will then be one line from the listing. You can then use a foreach loop to cycle through the array and test each element with a regular expression to see if it is equal to "core". If it is, do what you need to do. If not, do something else.
              The back tics, by the way, are another way of executing system commands.

              2. Instead of testing the variable above, you are setting it equal to the value. The equal sign, by itself, is an assignment operator. You would need to use either "==" (for numbers and integers) or "eq" (for strings and text).

              For my suggestion above though, you will be testing each element in the array against a Regular Expression to see if it is equal to the word core.


              I highly suggest that you really read a book on perl so that you can code better. Also, when writing a perl script, you reallly need to include the pragmas "use strict" and "use warnings". They will help eliminate all of the silly nonsensical errors that us coders make and you will also find that most people here won't do much with your code if you don't use them.

              That said, here is an untested bit of code to do what you want (and what I explained above):

              [code=perl]

              use strict;
              use warnings;

              my $dumpdir = "/tmp";

              my @listing = `ls $dumpdir`;

              foreach my $line (@listing)
              {
              if($line =~ m/core/)
              {
              print("core file found in $dumpdir");

              # pstack is tool in solaris to read core dump
              system("pstack ${dumpdir}/core >test.txt");
              }
              }

              [/code]

              Now, I could have put a print statement in an else to print "no core found", but it would have printed that for EVERY single line that it cycled through. Putting it in is completely up to you.

              I hope this works for you or gets you on the right path. I really suggest reading the book at that link so you can get the basic concepts down.

              Regards,

              Jeff

              Comment

              Working...