grep a string from 2 text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a string from 2 text files
# 1  
Old 04-22-2008
Lightbulb grep a string from 2 text files

How to grep a string in two different files,

I am having the following scenario

TextFile1
Code:
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
Code:
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
# 2  
Old 04-22-2008
simple solution (or have i misunderstood probelm)

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

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

then grep this:

Code:
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

Last edited by Yogesh Sawant; 04-22-2008 at 09:16 AM.. Reason: added code tags
# 3  
Old 04-22-2008
uhh sorry - typo noticed

the [-9] should be a [0-9], sorry
# 4  
Old 04-22-2008
Does this help ??

Code:
$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

# 5  
Old 04-22-2008
Quote:
Originally Posted by Shivdatta
Does this help ??

Code:
$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 :
Code:
awk '$5==0 && $3=="ServerA"{print $1}'  file1 file2 | sort -t/ -r -k3,3 -k2,2 -k1,1 | head -1

Jean-Pierre.
# 6  
Old 04-22-2008
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.

Code:
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.
# 7  
Old 04-22-2008
(Double post because of server timeout, sorry.)

Last edited by era; 04-23-2008 at 02:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

2. Shell Programming and Scripting

Grep 9 string text

I have a file that have text like. Download ABCD Edfdf x8634 4343 554545 (64bit) V33834-01 1.4M Download ABCD Edfdf x86 (64bit) V33855-01 1.4M Download ABCD Edfdf x8634 4343 5545 (64bit) V33834-01 1.4M i want text starts with V*-01 from above file. (2 Replies)
Discussion started by: learnbash
2 Replies

3. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

4. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

5. Programming

Help to grep string in log files

hello guys.., i have some problem with grepping strings in log files.. so..,i need an java logic on how to grep srtings in log files the output must be in gui.., please help me .. regards raghuraipur:confused: (1 Reply)
Discussion started by: raghuraipur
1 Replies

6. Shell Programming and Scripting

sh to grep and replace text files

Hello I was wondering what is a way you can put in your code that acts like goto for bash in windows ? lets say i have a if yes or no question how would i go about doing that? Also how can i search for text inside a file and insert my text into that part if you dont mind me asking? (5 Replies)
Discussion started by: william palmer
5 Replies

7. UNIX for Dummies Questions & Answers

Copy or Grep all text below a string

Hello, I am trying to copy all the text from a file below a search string... For example i want to grep all text below the word sure: UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! ... (2 Replies)
Discussion started by: aliaa2a
2 Replies

8. Solaris

grep a string on all files in the subdirectories

Hi all, I would want to find a string 'abc' on all files which is located under a directory. But inside the directory there are subdirectories, so how can I find whether 'abc' exists on all files in the subsequent subdirectories? Thanks. (4 Replies)
Discussion started by: *Jess*
4 Replies

9. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question