473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

grep program for unix

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
6 2068
KevinADC
4,059 Expert 2GB
I'm sure someone can help, where are you stuck?
Jan 5 '08 #2
just for starters

grep (some statement) > redirect file

I guess i need more info
Jan 5 '08 #3
numberwhun
3,509 Expert Mod 2GB
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
3,509 Expert Mod 2GB
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
lduggi
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
3,509 Expert Mod 2GB
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

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

Similar topics

13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
2
by: John E. Jardine | last post by:
Hi, Problem: Executing 's///' has a side effect on grep null string matching. If line 62, the substitution, is executed the last two values returned by grep and printed on lines 68, 69 are...
1
by: Al Belden | last post by:
Hi all, I've been working on a problem that I thought might be of interest: I'm trying to replace some korn shell scripts that search source code files with perl scripts to gain certain features...
1
by: Dirk Segelhorst | last post by:
Hi, I am looking for an efficient XML grep tool (similar to UNIX grep) for XPath search in big XML documents. The tools I found are not what I actually need: - xmlgrep...
3
by: E.U. | last post by:
Hello, I need to program grep (like the one UNIX has but more simple) For example if the programs name(the grep I will write) is p3 then if I write in shell p3 story.txt word The output...
9
by: gyandwivedi | last post by:
what should I use in perl /in place of grep -v in unix.
3
by: soniamadtha | last post by:
hello everyone, i have the following values : 98000345 and i would like to search this value in all of the sql files in the directory /temp/*sql normally on unix its grep( ' 98000345 '...
14
by: Rishi | last post by:
i want to frame a grep command which is successful if and only if the line contains ONLY alphabets and spaces no special characters and digits are allowed
6
by: kronus | last post by:
Hi everyone, This is my first time posting to the UNIX form and it might be a strange request, but I believe I have most of the pieces. Here's the overall goal -- I am trying to find the links...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.