Grep From Files and Display the File Name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep From Files and Display the File Name
# 1  
Old 07-30-2013
Network Grep From Files and Display the File Name

Their are lot of files under the dir prod. I would like to get the file name which contain the word hello.


File name firstprog.txt
Dir structure : t01/Admin/prod

I am using the below and does not work
/t01/Admin# grep -l hello /t01/Admin/* ( I am in the Admin dir and grep)

/t01/Admin/prod# grep -l hello /t01/Admin/prod ( I am in the prod dir and grep)

If I do a grep from /t01/Admin/prod# grep hello *.txt I get output firstprog.txt
# 2  
Old 07-30-2013
Perform a recursive search in the directory (gnu grep only AFIK)
Code:
grep -rl hello /t01/Admin

Or use find to grep each file, (works on all unix variants AFAIK)
Code:
find /t01/Admin -type f -name \*.txt -exec grep -l hello {} \;

# 3  
Old 07-30-2013
This is what I get when I used grep -rl hello /t01/Admin

grep: illegal option -- r
Usage: grep -hblcnsviw pattern file . . .

Using find /t01/Admin -type f -name \*.txt -exec grep -l hello {} \; I did not get any output.

Thanks for the quick response.
# 4  
Old 07-30-2013
2 questions,
Is Admin a symlink, because the find command above works for me on Linux and Solaris...
what happens when you remove the -name \*.txt parameter?
# 5  
Old 07-30-2013
Admin is symlink ( lrwxrwxrwx). No output if the emove the name \*.txt parameter.
This is the OS release and Version I am using SunOS ssunas995 5.10 Generic_118833-36 sun4u sparc SUNW,Sun-Fire-V445
# 6  
Old 07-30-2013
If Admin is a symlink, try...
Code:
find /t01/Admin/ -type f -name \*.txt -exec grep -l hello {} \;

Edit The Posix definition stipulates that when you refer to a symlink to a directory without the trailing slash it is the link "file" itself that you are addressing rather than the target

Last edited by Skrynesaver; 07-30-2013 at 01:09 PM..
# 7  
Old 07-30-2013
Great it worked. Thanks a lot. Appreciate the time you spent.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display lines for a particular year in a file using grep

hi, I have a log file with data for more than 3 years, i want only the rows for the year 2017, say for example. My file has the data like this 08-OCT-2015 11:17:35 AAA, BBBB 08-OCT-2017 11:17:35 AAA,Bdfdfd,dfdfd,dfd 08-Nov-2017 11:17:35 AAA,Bdfdfd,dfdfd,deree i want the rows... (2 Replies)
Discussion started by: skoshekay
2 Replies

2. UNIX for Dummies Questions & Answers

Display files based on particular file timestamp

Hi, I have requirement to list out files that are created after particular file. ex. I have below files in my directory. I want to display files created after /dirdat/CG1/cg004440 file. ./dirdat/CG1/cg004438 09/07/14 0:44:05 ./dirdat/CG1/cg004439 09/07/14 6:01:48 ... (3 Replies)
Discussion started by: tmalik79
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

4. Shell Programming and Scripting

Grep to display file name along with content in Solaris

Am using the following grep to match a particular patter in grep. grep xyz abc.txt now while i run this command, if the pattern matched, am getting the line containing xyz Output: xyz is doing some work Now if i want the file name also along with my output, what should i do Expected... (2 Replies)
Discussion started by: rituparna_gupta
2 Replies

5. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. Shell Programming and Scripting

korn shell how to solve rm file and display files

Write a KSH script called cleanse which displays the name of each file in a given directory and allows the user to interactively decide whether or not to keep or delete the specific file. Notes: Again, please check for errors. can any one help on this problem ?? (1 Reply)
Discussion started by: babuda0059
1 Replies

8. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

9. Shell Programming and Scripting

grep and display lines from a file

I have to grep on a few words in a file and then display the line containing those words and the line above it. For ex - File1.txt contains... abc xyz abc This is a test Test successful abc xyz abc Just a test Test successful I find the words 'Test successful' in the file... (6 Replies)
Discussion started by: user7617
6 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question