Search strings from a file in files in a directory recursively; then print the string with a status


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search strings from a file in files in a directory recursively; then print the string with a status
# 1  
Old 03-20-2019
Search strings from a file in files in a directory recursively; then print the string with a status

Hi All,

I hope somebody would be able to help me.
I would need to search a string coming from a file, example file.txt:
Code:
dog
cat
goat
horse
fish

For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.
And then should be able to create a status report file based from the result of the search, and produce the following format:
report.txt
Code:
dog|found
cat|found
goat|found
horse|missing
fish|missing

Thanks in advance!
# 2  
Old 03-20-2019
As with all shell scripting questions: which shell (and version) do you use? Which OS (and version) do you use? And, most important of all: what have you tried already?

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 03-20-2019
HI, I understand correctly?
Code:
grep -rowFf file ./

--- Post updated at 18:53 ---

I understood, it is necessary to deduce the absence of coincidences too.
# 4  
Old 03-20-2019
Quote:
Originally Posted by bakunin
As with all shell scripting questions: which shell (and version) do you use? Which OS (and version) do you use? And, most important of all: what have you tried already?

I hope this helps.

bakunin
Hi! It is an Oracle Linux Server 7.1.
I have tried the usual grep commands I can search in the site, but I have yet to see a mechanism that reads a file for strings (probably should be done using a loop) and find it recursively in a directory then prints a status.
I am still searching though.
Thanks!
# 5  
Old 03-20-2019
I dare to suggest this monster Smilie
Code:
grep -vFf <(grep -rowFf file.txt ./ | sort -t: -uk2 | cut -d: -f2 |
        awk '{print; print $0 "|found" > "report.txt"}') file.txt |
        sed 's/\([^:]*\)/\1|missing/' >> report.txt

--- Post updated at 20:40 ---

Code:
grep -vFf <(grep -rowFf file.txt ./ |
        awk -F: '!t[$2]++ {print $2; print $2 "|found" > "report.txt"}') file.txt |
        sed 's/\([^:]*\)/\1|missing/' >> report.txt

--- Post updated at 20:55 ---

Quote:
...regardless of case.
I forgot about the option "-i" in command "grep"
This User Gave Thanks to nezabudka For This Post:
# 6  
Old 03-20-2019
I LOVE monsters like nezabudka's above!


Still I'd like to strip it down a bit, given your system allows for "process substitution" and the relevant / applied grep options. Try:
Code:
grep -hirof file.txt * | sort -u | tee >(grep -vif- file.txt | sed 's/$/|missing/') | sed '/|/!s/$/|found/'

This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-20-2019
Hi RudiC
I tried through "tee" output to the stderr, hence the my first varient with "cut", but I did not succeed.
I will write your code in my notes. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search directory for string and print file name

Good Evening Folks - I have a need to search a specific directory and ALL sub-directories for the following string: Data Load UpdatedIf this string is found, I need to print content from the line directory above it between symbols, as well as the file name where it is found. Here is a... (1 Reply)
Discussion started by: SIMMS7400
1 Replies

2. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. UNIX for Advanced & Expert Users

Recursively search the string from a column in no. of files

i have a file named keyword.csv(contains around 8k records) which contains a no. of columns. The 5th column contains all the keywords. I want to recursively search these keywords in all .pl files(around 1k) and display the filename....Afterthat i will use the filename and some of the column from... (3 Replies)
Discussion started by: millan
3 Replies

5. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

6. Shell Programming and Scripting

How to recursively search for a list of keywords in a given directory?

Hi all, how to recursively search for a list of keywords in a given directory?? for example: suppose i have kept all the keywords in a file called "procnamelist" (in separate line) and i have to search recursively in a directory called "target/dir" if i am not doing recursive search then... (4 Replies)
Discussion started by: neelmani
4 Replies

7. UNIX for Dummies Questions & Answers

How to search two strings in a file and print the contents in between to a file

I have a file called po.txt. Here is the content of the file: <!DOCTYPE PurchaseOrderMessage (View Source for full doctype...)> - <PurchaseOrder> - <Header> <MessageId>cdb3062b-685b-4cd5-9633-013186750e10</MessageId> <Timestamp>2011-08-01T13:47:23.536-04:00</Timestamp> </Header> -... (4 Replies)
Discussion started by: webbi
4 Replies

8. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

9. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 Replies

10. UNIX for Dummies Questions & Answers

Unix find command to print directory and search string

Hi i need to print pathname in which the string present using 'find' command sample output like this Pathname String to be searched ---------- -------------------- /usr/test/myfile get /opt/test/somefile get Thanks in... (4 Replies)
Discussion started by: princein
4 Replies
Login or Register to Ask a Question