Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching inforamtion in a text file.

Status
Not open for further replies.

timely2

MIS
Oct 11, 2005
17
US
I am wanting to do a search for instances of a given string but I want more then just the line its on, but may three entry lines before and after each time I find the string. The file I look through is an over 5,000,000 line log. I only get the line the string is on with a basic grep and doing a tail and head takes alot of work and time.

Thanks,
Tim Ely
 
This has been handled before: Here's a reference that should get you started:

faq822-6173

Hope that helps. If that doesn't help, check out the Unix Scripting forum for other examples.

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
I tried the script they had but I kept getting a bad number error for the math part. I can get the part that does the grep to work and I could get the tail / head part to work but when I tried to have it look at every instance with the number of lines it would error out with the bad number.
 
If you have Perl, try the following script out:-


#!/usr/bin/perl

# Usage: cgrep [-lines] pattern [files]

$context = 3;

# They might want more or less context.

if ($ARGV[0] =~ /^-(\d+)$/) {
$context = $1;
shift;
}

# Get the pattern and protect the delimiter.

$pat = shift;
$pat =~ s#/#\\/#g;

# First line of input will be middle of array.
# In the eval below, it will be $ary[$context].

$_ = <>;
push(@ary,$_);

# Add blank lines before, more input after first line.

for (1 .. $context) {
unshift(@ary,'');
$_ = <>;
push(@ary,$_) if $_;
}

# Now use @ary as a silo, shifting and pushing.

eval <<LOOP_END;
while (\$ary[$context]) {
if (\$ary[$context] =~ /$pat/) {
print "------\n" if \$seq++;
print \@ary,"\n";
}
\$_ = <> if \$_;
shift(\@ary);
push(\@ary,\$_);
}
LOOP_END

 
Hi

If you accidentally have GNU [tt]grep[/tt] :
man grep said:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching
lines. Places a line containing -- between con­
tiguous groups of matches.

-B NUM, --before-context=NUM
Print NUM lines of leading context before matching
lines. Places a line containing -- between con­
tiguous groups of matches.

-C NUM, --context=NUM
Print NUM lines of output context. Places a line
containing -- between contiguous groups of matches.

Feherke.
 
Not sure on the perl how to read that but I was messing with the grep and I found that it does not want to see: grep -n "test" file.log | cut -d: f1

as a number. I broke it up into diffrent parts and seemed to work until you tried to use that part to do math.

Thanks
 
Oh, another part that could effect this is multiple lines will come back when I do a serch.
 
Maybe something like this:

Code:
for line in `awk '/[i][b]search_string[/b][/i]/{START=NR-3 ; FINISH=NR+3} END {while(START <= FINISH){print START ; START++}}' [i][b]filename[/b][/i]`
 do
        cat -n testfile | grep "${line} " 
 done

Keep in mind that I am no programmer and that there are much better ways to do this.

Regards,
Chuck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top