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
# 8  
Old 05-14-2014
If you're OK with awk instead of grep, something like this might work. You just have to make sure to enter the "target" in all lower case letters or add a "BEGIN" clause to translate it to lower case.

Code:
gawk -v target='dest_ip' 'NF { tot[FILENAME] += split(tolower($0),parts,target) - 1; } END { for(fn in tot) printf "%s %d\n", fn, tot[fn]; }' *

# 9  
Old 05-14-2014
Hi, Thank you xbin & Corona!

I put the code into a shell which is in the same folder as my textfiles. I execute it and it works! (With . showlines.sh)

However I'm not sure about the placement of the shell, my path is:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Should I put it in any bin or have it in a new ~/bin ?

And then I don't have to every time write the path of the shell script hopefully?

Some newbe questions...

/
with best regards,
Omar K N
Stockholm, Sweden
# 10  
Old 05-14-2014
You shouldn't put your own scripts into the system paths. Make a /home/myusername/bin, put it there, and add /home/myusername/bin to your PATH.
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 05-14-2014
There is an easier solution. What about the following?
Code:
grep -ci word * | grep -v ":0$"


Last edited by Franklin52; 05-15-2014 at 05:09 AM.. Reason: Please use code tags
This User Gave Thanks to gandolf989 For This Post:
# 12  
Old 05-14-2014
even better!
Thx gandolf989,

/
 
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