Unix Shell Script Help - Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Shell Script Help - Grep
# 1  
Old 09-26-2008
Unix Shell Script Help - Grep

Hi,

I am new to UNIX Shell scripting, and will require some help.

I am trying to search a directory with a number of files for a specific word, then if that word is found in any of the files, then the filename is appended to a log file or result file by ">" command.

I have been trying to use grep, but so far when i Grep for word in a file, it outputs the word but not the file name.

I need to do this search to a directory with numerous files. For example if i am looking for the word "UBO888.gnt" in a directory full of files, i try the following, but doesnt work...

for file in `ls`
do
grep "UBO888.gnt" $file > outputresults.txt
done;

How do I search and output filenames?

Any help is appreciated.

Thanks.
# 2  
Old 09-26-2008
use grep -l option..
# 3  
Old 09-26-2008
you dont need to use loop here.

Code:
grep -l "UBO888.gnt" /directorypath/* >logfile

if you are in same directory where all files are present then simply.
Code:
grep -l "UBO888.gnt" * >logfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I want to grep the text value in UNIX shell file

Hi Guys, I want to grep the text/code value in my shell script. Sample Code:-H "fjkldj skdfjk2342 kj s2323" Here, the code value is in double quotes after -H. This value I have to pic and put it in variable to pass a parameter value. Can any one help me on this! Thanks in... (13 Replies)
Discussion started by: lakshmanraok
13 Replies

2. Shell Programming and Scripting

New Unix user with shell script question using grep

Hello, I am a new Unix user and new to shell programming. I am working on a script to go through a log file and find the text error: grep -i 'error' monplus.mplog if I find the text error in the log file I would like to echo a message to the operator staing there is an error I am currently... (2 Replies)
Discussion started by: dtracy01
2 Replies

3. Shell Programming and Scripting

Grep in Shell script

hi guys very new to this game so excuse my ignorance. I need to create a script that simply greps for a text string and then outputs a message depending on whether the text string is there or not. The script I have setup is below, but whenever I run it I get the following error: ... (5 Replies)
Discussion started by: ap2112
5 Replies

4. Shell Programming and Scripting

Shell Script with Grep

Hi guys - below is my script that is checking for current file, size and timestamp. However I added a "grep" feature in it (line in red), but not getting the desired result. I am trying to acheive in output: 1. Show me the file name, timestamp, size and grep'ed words It would be a... (2 Replies)
Discussion started by: DallasT
2 Replies

5. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

6. Shell Programming and Scripting

Shell script grep help

Hey there, newbie question : echo "::kmastat" | /usr/bin/mdb -k | grep Total | grep "kmem_*" Total 17326080 432853 0 Total 426508288 65458 0 Total 704757760 1572001732 0 Total ... (11 Replies)
Discussion started by: shriyer
11 Replies

7. UNIX for Dummies Questions & Answers

Using Grep within a UNIX Script

Hi, I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated! -------------------- set xTest = 'grep -i lisa daily.log' if ; then echo "TEST" >> daily.log fi... (6 Replies)
Discussion started by: lisa_0801
6 Replies

8. Shell Programming and Scripting

grep in Shell script

Hello I do want to write a script which will check any errors say "-error" in the log file then have to send email to the concern person . And the concern person will correct the error . Next time if the script runs eventhough the error has been corrected it will ... (1 Reply)
Discussion started by: Krishnaramjis
1 Replies

9. Shell Programming and Scripting

Unix shell script (grep -A 6 -B 2 "ORA-" filename

BACKGROUND: I am using Solaris 10. Some of my boxes have gnu grep and I can use -A and -B flags on those. However, the solaris flavor of grep won't use the flags -A or -B. And some of my boxes won't be getting gnu grep. Should I try using perl, awk, or sed? Actual PROBLEM: I am... (7 Replies)
Discussion started by: el_guero
7 Replies

10. Shell Programming and Scripting

Using Grep in a Shell Script

Hi everyone, Im trying to write a Shell script that basically creates a set of files based on a file with many records. For example if a file called dummy has the following content: a.txt 1st line of a's text file 2nd line of a's text file 3rd line of a's text file b.txt 1st line of b's... (8 Replies)
Discussion started by: nbvcxzdz
8 Replies
Login or Register to Ask a Question