Computing.Net > Forums > Unix > print file name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

print file name

Reply to Message Icon

Name: cesar_NZ
Date: October 5, 2008 at 20:04:56 Pacific
OS: solaris
CPU/Ram: na
Product: na
Comment:

hi All,
have a script which searches log files looking for some text. can someone tell me how to print the file name only if the text is in that particular file ?? currently my script prints out all the file names it goes through :

#!/bin/ksh
find ./ -name "SystemOut*.log"|while read LINE; do grep "$1 : OES \[LOGIN\]" $LINE; echo "$LINE"; done


thanks in advance,
Cesar.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 6, 2008 at 18:31:29 Pacific
Reply:

Why don't you use the grep -l option?


0

Response Number 2
Name: cesar_NZ
Date: October 6, 2008 at 19:39:21 Pacific
Reply:

because it doesnt work :)


0

Response Number 3
Name: Vasanth
Date: October 7, 2008 at 08:38:51 Pacific
Reply:

You can use echo $? after and if its success print the values .

Vasanth


0

Response Number 4
Name: cesar_NZ
Date: October 7, 2008 at 15:36:39 Pacific
Reply:

hi Vasanth, sorry didnt understand your reply. did you mean to say check if the grep command was successful then print the line ?

i.e

if [ "$?" -ne "0" ]; then
echo $LINE;
fi


if so, this wont work as the grep command will be successful, it just wont find any results and print out the file name which is what i am trying to avoid.


thanks.


0

Response Number 5
Name: cesar_NZ
Date: October 7, 2008 at 15:38:14 Pacific
Reply:

ooops meant to say :


if [ "$?" -eq "0" ]; then


0

Related Posts

See More



Response Number 6
Name: Vasanth
Date: October 12, 2008 at 01:31:15 Pacific
Reply:

I just tried simple command in my box to check the return status and its perfectly worked

$ cat a.txt
I am able to grep

$ grep am a.txt
I am able to grep
$ echo $?
0

$ grep ap a.txt
$ echo $?
1

In ur case it will successful means it always return some values ?

Vasanth


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: print file name

Extracting File names www.computing.net/answers/unix/extracting-file-names/4597.html

csh - if: Missing file name www.computing.net/answers/unix/csh-if-missing-file-name/4201.html

Add file name end of line in every row www.computing.net/answers/unix/add-file-name-end-of-line-in-every-row/8512.html