Search for string and display those NOT found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for string and display those NOT found
# 1  
Old 05-19-2007
Search for string and display those NOT found

In my script I read a input file and search all the files in a directory and it's sub-directories for that string using:

find . -type f -print | xargs grep $var1

This just displays all the lines the string was found on. Too much data.

What I need is to store in a file one time those strings that have not been found. So if I'm searching for 800 strings and 20 of those are not in the any file of the directories I'm searching, than I want a output file with those 20 search strings in it.

Thanks in advance,

John
# 2  
Old 05-19-2007
not sure if its what you want, but you can add -v option in grep command.
# 3  
Old 05-19-2007
Search for string and display those NOT found

Thanks for the reply. No, -v would not work because the searched lines are 300-400 char. long. I just need to know what search strings were not found, which are under 15 char. So if I could get them outputed to a file I can work with it.

John
# 4  
Old 05-19-2007
its not very clear (to me).. in your first post, you said you are reading an input file and then search files in a directory and its subdirectories? what is in that input file??
can you describe more clearly? give examples where appropriate..
# 5  
Old 05-19-2007
The following awk program display all the strings that have not been found in any of the input files. The strings are to be searched are read from the first input file.
Code:
#!/usr/bin/awk -f
# Filename: not_found

#
# Strings file.
#

NR==FNR {
   string_found[$1] = 0; # 0 = No, >0 = Yes
   next;
}

#
# New data file.
# Build array with strings not yet found
#

FNR==1 {
   for (str in string_found) {
      if (string_found[str] == 0) {
         strings[str]++;
         strings_count++;
      }
   }
   if (strings_count == 0) exit;
}

#
# Input data;
# Search data for strings not yet found
#

{
   for (str in strings) {
      if ($0 ~ str) {
         string_found[str]++;
         delete strings[str];
         if (--strings_count == 0) exit;
      }
   }
}

#
# No more files or allstrings have been found
# Print strings not found
#

END {
   for (str in string_found)
      if (string_found[str] == 0) print str;
}

The file string_list contains the strings to be search.
The file strings_not_found will contain the strings that have not be found in any of the files.
Don't put files string_list and strings_not_found in one of the directories that you want to scan.

Code:
chmod +rx not_found
not_found string_list $(find . -type f) > strings_not_found

Jean-Pierre.
# 6  
Old 05-21-2007
GHOSTDOG74 writes:

== its not very clear (to me).. in your first post, you said you are reading an == input file and then search files in a directory and its subdirectories? what == is in that input file??
== can you describe more clearly? give examples where appropriate..

Here is what my input file format looks like for 4 lines. It's called padlist.

CL1ACC01,"-13181,-32907"
CL1AN01,"-8077,-12652"
CL1CP01,"-13181,4"
CL1FTP01,"-2200,0"

My script is:

exec < /home/padlist
while read d1 d2
do
find . -type f -print | xargs grep $d1 | more
done

So the about displays the full line from the file where $d1 was found. I just need to know what strings were not found.
# 7  
Old 05-21-2007
Hammer & Screwdriver

Quote:
Originally Posted by aigles
The following awk program display all the strings that have not been found in any of the input files. The strings are to be searched are read from the first input file.
Code:
#!/usr/bin/awk -f
# Filename: not_found

#
# Strings file.
#

NR==FNR {
   string_found[$1] = 0; # 0 = No, >0 = Yes
   next;
}

#
# New data file.
# Build array with strings not yet found
#

FNR==1 {
   for (str in string_found) {
      if (string_found[str] == 0) {
         strings[str]++;
         strings_count++;
      }
   }
   if (strings_count == 0) exit;
}

#
# Input data;
# Search data for strings not yet found
#

{
   for (str in strings) {
      if ($0 ~ str) {
         string_found[str]++;
         delete strings[str];
         if (--strings_count == 0) exit;
      }
   }
}

#
# No more files or allstrings have been found
# Print strings not found
#

END {
   for (str in string_found)
      if (string_found[str] == 0) print str;
}

The file string_list contains the strings to be search.
The file strings_not_found will contain the strings that have not be found in any of the files.
Don't put files string_list and strings_not_found in one of the directories that you want to scan.

Code:
chmod +rx not_found
not_found string_list $(find . -type f) > strings_not_found

Jean-Pierre.
Jean_Pierre,
I don't know where I would place my code within yours to make this all work. Can you help me? I have:

exec < home/padlist
while read d1 d2
do
find . -type f -print | xargs grep $d1 | more
done

This just prints to the display the lines where the search was found.

Thanks,

John
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search string in multiple files and display column wise

I have 3 files. Each of those files have the same number of records, however certain records have different values. I would like to grep the field in ALL 3 files and display the output with only the differences in column wise and if possible line number File1 Name = Joe Age = 33... (3 Replies)
Discussion started by: sidnow
3 Replies

2. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

4. Shell Programming and Scripting

Search for a tag and display a message if not found.

Hi All, I am working with a XML file. Below is part for the file. <Emp:Profile> <Emp:Description>Admin</Emp:Description> <Emp:Id>12347</Emp:Id> </Emp:Profile> <Emp:Profile> ... (7 Replies)
Discussion started by: Girish19
7 Replies

5. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

6. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

7. Shell Programming and Scripting

Search a String and display only word.

Hello Gurus, Apologies if this Q has been repeated but i was not able to find it :( I have an input file: ------------------------------- Replace DB.Employee as select column1 column2 from DB_T.Emp and DB.Test and DB.Dept and DB_T.Ter; ------------------------ (4 Replies)
Discussion started by: indrajit_u
4 Replies

8. UNIX for Advanced & Expert Users

Parsing String, Search then display rows

Get occurence of "open" considering duplicates(get the last open). Once you are pointing to the last open count 2 rows to get the correct data. Every begin and end statement, there is a "close" and "open". There can be many "close" and "open" within the begin and end statement but we are... (9 Replies)
Discussion started by: buddyme
9 Replies

9. Shell Programming and Scripting

Display only found string

Is there a way for grep to output only the found string and not the whole line? I have a ksh script which reads in a file and loops through every line looking up on a grep -f list. For it to only display only the string found i pass this to awk as a variable and loop through the list file using... (5 Replies)
Discussion started by: Cranie
5 Replies

10. Shell Programming and Scripting

Recursive Search and replace only when found string

Hello all ( again ) I will like to search and replace string in text file ok I can loop throw the files like : foreach f ( ` find . -name "*."`) .. but here I like to examine the file if in contain the desired string and so do the sed -e 's/blah/foo/g' thingy on it or there is better way... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question