By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

grep program for unix

P: 2
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
Jan 5 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
I'm sure someone can help, where are you stuck?
Jan 5 '08 #2

P: 19
just for starters

grep (some statement) > redirect file

I guess i need more info
Jan 5 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jan 5 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jan 5 '08 #5

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. $file = "core" ;
  2. if($file = core)
  3. {
  4. system("pstack core >test.txt");  ---pstack is tool in solaris to read core dump.
  5. print( "core file found in a system");
  6. }
  7. else
  8. {
  9. print("core file not found in a system");
  10. }
  11.  
or is any effient way to write same program
Jan 6 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
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:

Expand|Select|Wrap|Line Numbers
  1. $file = "core" ;
  2. if($file = core)
  3. {
  4.  
  5. print( "core file found in a system");
  6. }
  7. else
  8. {
  9. print("core file not found in a system");
  10. }
  11.  
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:

Expand|Select|Wrap|Line Numbers
  1. my @listing = `ls <directory_path>`;
  2.  
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):

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5. my $dumpdir = "/tmp";
  6.  
  7. my @listing = `ls $dumpdir`;
  8.  
  9. foreach my $line (@listing)
  10. {
  11.     if($line =~ m/core/)
  12.     {
  13.         print("core file found in $dumpdir");
  14.  
  15.         # pstack is tool in solaris to read core dump
  16.         system("pstack ${dumpdir}/core >test.txt");
  17.     }
  18. }
  19.  
  20.  
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
Jan 6 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.