grep a string from 2 text files

How to grep a string in two different files,

I am having the following scenario

TextFile1

Date (dd/mm)Time      Server        IP                       Error Code
===========================================================================
10/04/2008   10:10     ServerA    xxx.xxx.xxx.xxx             6	
10/04/2008   10:10     ServerB    xxx.xxx.xxx.xxx             9	
10/04/2008   10:10     ServerC    xxx.xxx.xxx.xxx             16	
10/04/2008   10:10     ServerD    xxx.xxx.xxx.xxx             60	

TextFile2

Date (dd/mm)Time      Server        IP                    Error Code
=======================================================================
09/04/2008   10:10     ServerA    xxx.xxx.xxx.xxx             0	
09/04/2008   10:10     ServerB    xxx.xxx.xxx.xxx             0	
09/04/2008   10:10     ServerC    xxx.xxx.xxx.xxx             0	
08/04/2008   10:10     ServerA    xxx.xxx.xxx.xxx             0	
08/04/2008   10:10     ServerB    xxx.xxx.xxx.xxx             0	
08/04/2008   10:10     ServerC    xxx.xxx.xxx.xxx             0	
07/04/2008   10:10     ServerA    xxx.xxx.xxx.xxx             0	
07/04/2008   10:10     ServerB    xxx.xxx.xxx.xxx             0	
07/04/2008   10:10     ServerC    xxx.xxx.xxx.xxx             0

grep the date for server A with error code = 0 into output.txt
the output should be the previous date or the latest in which the error code =0

output.txt
09/04/2008

1st - cat all text files together into 1 huge txt file (strating with most recent date 1st)

cat file1.txt file2.txt file3.txt > output1.tmp

then grep this:

grep 'ServerA *[0-9][0-9]*\.[0-9][0-9]*\.[0-9][-9]*\.[0-9][0-9]*  *0' output1.tmp

it looks nasty but the spaces, stars, [0-9]s, backslashes, full stops and spaces are needed to cope with the IP addresses where any number of integeres must be accepted - note before the last *0 there are 2 spaces - this is not a typo - the star means 'zero or more occurrences of the previous character' (in this case a space) - adding an extra space ensures 1 or more spaces are read

the [-9] should be a [0-9], sorry

Does this help ??

$cat file1
10/04/2008 10:10 ServerA xxx.xxx.xxx.xxx 6
10/04/2008 10:10 ServerB xxx.xxx.xxx.xxx 9
10/04/2008 10:10 ServerC xxx.xxx.xxx.xxx 16
10/04/2008 10:10 ServerD xxx.xxx.xxx.xxx 60

$cat file2
09/04/2008 10:10 ServerA xxx.xxx.xxx.xxx 0
09/04/2008 10:10 ServerB xxx.xxx.xxx.xxx 0
09/04/2008 10:10 ServerC xxx.xxx.xxx.xxx 0
08/04/2008 10:10 ServerA xxx.xxx.xxx.xxx 0
08/04/2008 10:10 ServerB xxx.xxx.xxx.xxx 0
08/04/2008 10:10 ServerC xxx.xxx.xxx.xxx 0
07/04/2008 10:10 ServerA xxx.xxx.xxx.xxx 0
07/04/2008 10:10 ServerB xxx.xxx.xxx.xxx 0
07/04/2008 10:10 ServerC xxx.xxx.xxx.xxx 0

$awk -F" "  '$5==0 && $3=="ServerA"{print $1}'  file1 file2 | sort -r | head -1

09/04/2008 

The same with a modified sort command :

awk '$5==0 && $3=="ServerA"{print $1}'  file1 file2 | sort -t/ -r -k3,3 -k2,2 -k1,1 | head -1

Jean-Pierre.

Incidentally, if you want to explore davenorm's solution, I believe there is really no need to cat the files. Just run grep on them all in one go.

grep -h 'ServerA *[0-9][0-9]*\.[0-9][0-9]*\.[0-9][-9]*\.[0-9][0-9]*  *0' file1.txt file2.txt file3.txt

The -h option is for suppressing the printing of the file name with the match. If your grep doesn't have that, perhaps it's acceptable to simply leave it off, or switch to e.g. sed which in principle can do the same search with a very minor couple of modifications, and doesn't print file names.

(Double post because of server timeout, sorry.)

Please click one of the Quick Reply icons in the posts above to activate Quick Reply.

Hi ,

only " sort -r " may create a if the dates are of differeny months or years.

That may work well if all the dates belong to same month and year.

correct me if iam wrong in the above case.

By the way, another solution,

grep -h ServerA file1 file2 | awk '{if ($5==0 ) {print $1}}' | sed 's#\(..\)/\(..\)/\(.*\)#\3\2\1#' | sort -r | head -1 | sed 's#\(....\)\(..\)\(..\)#\3/\2/\1#'

09/04/2008

multiple posts bcoz of server problem...sorry

multiple posts bcoz of server problem...sorry

Any way to append this output 09/04/08 at the end of file1.txt in the same column

command which produces output >>file_to_be_appended

i want to add at end of the column and not end of last line

File1.txt

Date (dd/mm)Time      Server        IP                  Error Code        Output
========================================================================================================
10/04/2008   10:10     ServerA    xxx.xxx.xxx.xxx              6           09/04/2008 (grepped from file2.txt)
10/04/2008   10:10     ServerB    xxx.xxx.xxx.xxx             9	
10/04/2008   10:10     ServerC    xxx.xxx.xxx.xxx             16	
10/04/2008   10:10     ServerD    xxx.xxx.xxx.xxx             60

THANKS to all for the replies

Sarcasm, huh? You are adding a significant new requirement which is hard to adapt into the previous solution, so I would suggest you start a new thread, with all your requirements up front this time. Feel free to include a pointer back to this thread for background.