1830231 Members
1701 Online
109999 Solutions
New Discussion

Re: shell script

 
SOLVED
Go to solution
Ionut Grigorescu_2
Super Advisor

shell script

Hi all,

I need a shell script which look in a directory and printout to a file only the names of the files that contain a specified string (for example "record 19").
Any idea?
If it weren't for STRESS I'd have no energy at all
11 REPLIES 11
Jean-Louis Phelix
Honored Contributor
Solution

Re: shell script

hi,

#!/usr/bin/sh
DIR="$1"
STRING="$2"
cd $1
grep -l "$STRING" $DIR/* 2>&-

Regards,

Jean-Louis.
It works for me (© Bill McNAMARA ...)
john korterman
Honored Contributor

Re: shell script

Hi,
you could try this:
# find -type f -print | xargs grep "record 19" | awk -F: '{print $1}' >outfile

regards,
John K.
it would be nice if you always got a second chance
Christian Gebhardt
Honored Contributor

Re: shell script

You don't need three processes using find:
find -type f -exec grep -il "record19" {} \;

Chris
Ionut Grigorescu_2
Super Advisor

Re: shell script

Jean: merci!
John: the output file was empty!
Christian: some output but not only filenames, also content!
If it weren't for STRESS I'd have no energy at all
Jean-Louis Phelix
Honored Contributor

Re: shell script

Ionut,

I think that any solution with find should also work and display only filenames if you use the '-l' grep option. The only problem is that it will always try to explore also any subdirectories, not only the starting points. The '-type f' limit only the grep, but not the subdirectory searches.

Regards,

Jean-Louis.
It works for me (© Bill McNAMARA ...)
john korterman
Honored Contributor

Re: shell script

Sorry, this should work:
# find . -path "./*" -prune|xargs grep "record 19" |awk -F: '{print $1}'

regards,
John K.


it would be nice if you always got a second chance
Chuck J
Valued Contributor

Re: shell script

In the last line of Jean-Louis' script just direct the output to a file:

grep -l "$STRING" $DIR/* > outputfile.out

Chuck J
H.Merijn Brand (procura
Honored Contributor

Re: shell script

Ionut asked for file-NAME's to be of a specific patter, though you all scan in the CONTENT's of the files. Ionut's reaction however shows that you interpreted that was indeed his wish

File names:

# find dir | grep -i pattern

File content, use GNU grep:

# grep -l -r pattern dir
Enjoy, Have FUN! H.Merijn
Ionut Grigorescu_2
Super Advisor

Re: shell script

Hi Procura,

I meant the files that contain a specific string,
not the names of the files that contain a specific string :-))
Anyway, thanx for your care!
If it weren't for STRESS I'd have no energy at all
Chia-Wei
Advisor

Re: shell script

Borrowing John's idea :

find . -name "*" |xargs grep "ksh" |awk -F: '{print $1}'

which cancel off the -prune, will enable the command to search further down the directory. This might be useful sometimes.
Ionut Grigorescu_2
Super Advisor

Re: shell script

I have tested all your ideas. The simpliest one is that grep -l "pattern" //* > output.file
In fact I don't need a shell script to do that. But it was a good example that in Unix there are more solutions for one problem.
If it weren't for STRESS I'd have no energy at all