Grep for list of files with only found hits


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep for list of files with only found hits
# 1  
Old 05-14-2014
Grep for list of files with only found hits

Hi,

With grep -ci word * I get a list of files either they have a hit, or the have not (0).

I wanted to pipe to a new list of files, which only shows the files where the string was found, and counted, not the whole bunch.

How to do this?

Any advice welcome!


with best regards,
Omar K N
Stockholm, Sweden
# 2  
Old 05-14-2014
Can you show an example.
And remember to wrap your commands and output with CodeTags - this makes it much easier to read.
# 3  
Old 05-14-2014
Use -l option (--files-with-matches)
# 4  
Old 05-14-2014
Thank you Yoda,

Yes grep -l shows a list of files-with-matches,
but it doesn't tell how many matches in each file.

So there has to be set in a count of some sort, maybe by piping from
Code:
grep -ci word * |*…

# 5  
Old 05-14-2014
May be something like this:
Code:
grep -ci word * | while read line; do
if [ ${line#*:} -gt 0 ]; then
 printf "%s\n" "$line"
fi
done

This User Gave Thanks to xbin For This Post:
# 6  
Old 05-14-2014
Alright, ... I see have to put this in a shell script first,-

/okn
# 7  
Old 05-14-2014
Actually no, you can type it into your shell and have it work too. It's the same thing.
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep everything between two pattern if match not found

I need to help to work this Print everything between 2 patterns if grep is not found the search word example Detroit orange cat bat rat apple sed -n "/Detroit,/apple/p" d |grep chicago output would be Detroit orange cat bat rat (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

List files in dir and grep accordingly

bash or c-shell On Solaris 10 I need a command that will list the files of a directory, and grep each of those filenames out of a static text file, and then only echo field 2 of the static text file. Something like this: #> ls -1 /home/dir/ | each filename grep $filename static.txt |awk... (4 Replies)
Discussion started by: ajp7701
4 Replies

4. Shell Programming and Scripting

Grep and replace with found string

Hi, I need to find all rows in 1st col of one file in another file (first occurrence) and replace the 1st col of first file with the grep result (the entire line). For example search AA from file 1 in file 2 and replace in file 1 by entire line found. File1 AA BB CC DD BB AA CC DDFile2 ... (2 Replies)
Discussion started by: ritakadm
2 Replies

5. Shell Programming and Scripting

I'm trying 2 copy files after using the grep to get a list

for XmlFileName in ${xmlFileNames} do XmlFileName=$(echo $XmlFileName | sed 's|./||') # Remove leading ./ path that find command prefixes to filenames cp $XmlFileName $NEW_DIR/ done (1 Reply)
Discussion started by: emc^24sho
1 Replies

6. Shell Programming and Scripting

grep: not found

Hi, running below korn shell script #!/bin/ksh chk_dump() { CNT=`ls *.dmp|grep $DATE|wc -l` if ; then echo "delete_dumpfile" else echo "start_exp" fi } chk_dump getting below error: (3 Replies)
Discussion started by: milink
3 Replies

7. Shell Programming and Scripting

Grep a files list

Hi, I want to grep a string from the list of files present in a text file. I want the output to be printed only when the string is present in a file along with the file name.. I tried some thing like cat files_list | grep "search_string" $_ I know $_ is wrong but i want something... (7 Replies)
Discussion started by: nigas
7 Replies

8. Homework & Coursework Questions

list of files modified 10 hours ago using grep

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Here is the question: Make a list of files in your home directory that were changed less that 10 hours ago,... (3 Replies)
Discussion started by: fight4love
3 Replies

9. Shell Programming and Scripting

Grep: Return a user message when no hits

I have ASCII files to parse that 48 hours old or more ; I can identify them like so find . -name "FILE*TXT" -mtime +1 -exec ls -ltas '{}' ';' Some files have a list of hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to... (3 Replies)
Discussion started by: alan
3 Replies

10. UNIX for Dummies Questions & Answers

list all files containing 4 digit number using grep

how can i list all files in my home directory that have a 4 digit id number, the line number where the id is located and the id itself not printing the entire line? (5 Replies)
Discussion started by: hobiwhenuknowme
5 Replies
Login or Register to Ask a Question