Help on searching for a string on multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on searching for a string on multiple files
# 1  
Old 04-17-2014
Help on searching for a string on multiple files

Hi all,

I am sure some gurus will find a better way of doing this. FYI, I've manually modified some of the data 'coz they are somewhat confidential, so there may be some typo errors.

At the moment, I have 3 files that I am trying to search for. Sometime in the future, it may go beyond 3 files.

From these 3 files, I have to search for whether an application service is found in the files or not. If the file contains it, that means the application service is running on that server, otherwise it is not.

At the moment, I am reading each file and then awk'ing the field for the application service to a variable. I then read each file and grep for this string.

At the moment, the script is working as it is but it seems complicated. I thought maybe someone from the FORUM can make it better in some way.

Below is the main part that awk the field and search for the application string on each file

Code:
 
 
ls -1 *server* > tmp.00
server_count=`wc -l tmp.00 | awk '{ print $1}'`
#cat /dev/null > service_to_check.tmp.00
while read file1
do
   server=`echo $file1 | awk -F"." '{ print $1 }'`
   while read file2
   do
      service=`echo $file2 | awk '{ print $2 }'`
      service_count=0
      service_count=`grep -il ${service} *server* | wc -l | awk '{ print $1 }'`
      #echo "... Checking ${server} : Service = ${service} : ${service_count}"
      if [[ ${service_count} < ${server_count} ]] ; then
         grep -il ${service} *server* | awk -F"." '{ print $1 }' | sort | uniq > service_node.tmp.00
         service_node_server=""
         while read service_node
         do
            service_node_server="${service_node} :: ${service_node_server}"
         done < service_node.tmp.00
         echo "${service} :: FOUND ONLY in --> ${service_node_server} :: "
      fi
   done < ${file1}
done < tmp.00

- Below is example of the files that I am comparing or trying to. Field2, test01_app is the one that I need to check if it exist on all the files. If it does not exist. For the sample files below, only test01_app exist on all files.

Code:
 
 
server1:
 
Service "test01_app.inq.com.ph" has 1 instance(s).
Service "test03_app.inq.com.ph" has 1 instance(s).
 
server2:
 
Service "test01_app.inq.com.ph" has 1 instance(s).
Service "test02_app.inq.com.ph" has 1 instance(s).
Service "test04_app.inq.com.ph" has 1 instance(s).
 
server3:
 
Service "test01_app.inq.com.ph" has 1 instance(s).
Service "test02_app.inq.com.ph" has 1 instance(s).
Service "test03_app.inq.com.ph" has 1 instance(s).
 
tmp.00
server1.inq.com.ph.txt
server2.inq.com.ph.txt
server3.inq.com.ph.txt


I believe the script is very beginner like. It works to some extent but I believe it can be better and there is one other thing that I can't figure out to do which is to also find where the string does not exist. There is a bit of 'cheating' involve as well I guess where the search is based on the grep count.

Note that there is two (2) while loops. This is because I won't know which of the files has all the services so I need to read them all using the outer while loop and the inner while loop is to check all files for the existence of the application string. tmp.00 contains the name of the files to compare, the files are named something like server1.inq1.com.ph.

On the code section below, while I am able to find the servers/files where the service string is not found, how do I print the servers/files where the service string is NOT FOUND? That is, instead of saying
Code:
echo "${service} :: FOUND ONLY in --> ${service_node_server} :: "

I would want to do
Code:
echo "${service} :: FOUND ONLY in --> ${service_node_server} :: NOT FOUND in --> ${service_node_server_not_found}"

. I can't find a grep option to do that. I've experimented with diff but can't get it to print in the format that I wanted it to.

Another useful thing to be able to do is to report where all files contain the application string, i.e. PASSED because it is on all server files. I believe that is just a matter of putting an else in the if-then.

Code:
 
      if [[ ${service_count} < ${server_count} ]] ; then
         grep -il ${service} *server* | awk -F"." '{ print $1 }' | sort | uniq > service_node.tmp.00
         service_node_server=""
         while read service_node
         do
            service_node_server="${service_node} :: ${service_node_server}"
         done < service_node.tmp.00
         echo "${service} :: FOUND ONLY in --> ${service_node_server} :: "
      fi


Any feedback will be much appreciated. Thanks in advance.
# 2  
Old 04-17-2014
Why cant you try something like below
Code:
if [ `grep -il test01_app *server* | wc -l` -eq 3 ]
then 
echo "test01_app found in all servers"
else
echo "test01_app not found in all servers"
fi

obviously you need to replace hard-coded values by your original values in above script
# 3  
Old 04-17-2014
you can also try like this for getting common from all three files .

Code:
awk -F "." '{if(FILENAME=="server1"){flag1[$1]=1;}else if(FILENAME=="server2"){flag2[$1]=1;}else{if(flag1[$1]==1 && flag2[$1]==1){print $0 >"common.txt"}}}' server1 server2 server3

here is the output files

Code:
> cat common.txt 
Service "test01_app.inq.com.ph" has 1 instance(s

)


or

for getting common and difference

Code:
cat server1 server2 server3 | sort | uniq > all_files #combining all three files. 
awk -F "." '{if(FILENAME=="server1"){flag1[$1]=1;}else if(FILENAME=="server2"){flag2[$1]=1;}else if(FILENAME=="server3"){flag3[$1]=1;}else{if(flag1[$1]==1 && flag2[$1]==1 && flag3[$1]==1){print $0 >"common.txt"} else{print $0 > "not_common"}}}' server1 server2 server3 all_files

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

Searching in multiple files

I am new to unix and I would like to search multiple log files to find earliest occurrence of a text. Ex: Say I have 10 logs file each ending with .log and I want to find the text “CustomeError” . I want to find the which log file “CustomeError” comes first and lines which surround’s ... (4 Replies)
Discussion started by: jim john
4 Replies

2. Shell Programming and Scripting

Searching for similar row(s) across multiple files

Hello Esteemed Members, I need to write a script to search for files that have one or more than one rows similar. Please note that there is no specific pattern that I am searching for. The rows can be different, I just need to find out two or more similar records in two or more files. There... (7 Replies)
Discussion started by: Yoodit
7 Replies

3. UNIX for Dummies Questions & Answers

Grep in Perl - Searching through multiple files

I'm attempting to use grep in Perl with very little success. What I would like to do in Perl is get the output of the following grep code: grep -l 'pattern' * This gives me a list of all the files in a directory that contain the pattern that was searched. My attempts to do this in Perl... (4 Replies)
Discussion started by: WongSifu
4 Replies

4. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

5. Shell Programming and Scripting

Searching a word in multiple files

Hi All, I have a issue in pulling some heavy records , I have my input file has 10,000 records which i need to compare with daily appended log files from (sep 1st 2009 to till date) . I tried to use grep fgrep and even sed , but the as time is factor for me , i cannot wait for 5 days to get the... (3 Replies)
Discussion started by: rakesh_411
3 Replies

6. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

7. UNIX for Dummies Questions & Answers

Searching for files with certain string pattern

Hello All I would like to search for files containing certain string pattern under all the directories under /vobs/vobname and print the output to a file in my home directory. How can I do this? Note: /vobs/vobname conatins several directories. Thank You in advance newbetounix (1 Reply)
Discussion started by: intrigue
1 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Searching for multiple criteria in log files?

I would like a simple shell script that will allow me to display to screen all unsuccessful su attempts in my sulog file, for the present date. I have been trying several different combinations of commands, but I can't quite get the syntax correct. The mess I have right now (don't laugh) is... (4 Replies)
Discussion started by: Relykk
4 Replies

10. Shell Programming and Scripting

Searching multiple files with multiple expressions

I am using a DEC ALPHA running Digital UNIX (formly DEC OSF/1) and ksh. I have a directory with hundreds of files that only share the extension .rpt. I would like to search that directory based on serial number and operation number and only files that meet both requirements to be printed out. I... (6 Replies)
Discussion started by: Anahka
6 Replies
Login or Register to Ask a Question