Search for multiple lines in large file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for multiple lines in large file
# 1  
Old 10-19-2009
Search for multiple lines in large file

Hi,
I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry.

STRING_TO_GREP="$1"
FILE_TO_GREP="$2"
NUMBER_OF_LINES_BEFORE=$3
NUMBER_OF_LINES_AFTER=$4

for i in `grep -n "${STRING_TO_GREP}" $FILE_TO_GREP|cut -d ':' -f1`
do

echo "Line Number $i"
echo "--------------"
i1=$(($i-NUMBER_OF_LINES_BEFORE))
i2=$(($i+NUMBER_OF_LINES_AFTER))

if [ ${i1} -le 0 ]; then
i1=1
fi

sed -n "${i1},${i2}p" $FILE_TO_GREP
done


But my problem is that I do not know the value of NUMBER_OF_LINES_BEFORE and NUMBER_OF_LINES_AFTER beforehand, but needs to be identified based on the occurence of another string S1 just before the occurence of STRING_TO_GREP.

For example, if the file F1.txt contains:
a
b
c
d
e
f
g
h
i
j
c

I want to retrieve the following text based on the grep for 'f' with 'c' as the start of text and 'i' as the end. Please note that I do not want to include the c given in the end of file.
c
d
e
f
g
h
i


Thanks & Regards,
Praveen
# 2  
Old 10-19-2009
hi, maybe you can use the -A an d -B option of grep, i put and example :
texto= is a file for example, has 6lines
man grep:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of
matches.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing -- between contiguous groups of
matches.

Code:
$more texto
line1
line2
line3
line4
line5
line6
$grep -A 2 -B 1 "line3" texto
line2
line3
line4
line5
$grep -A 1 -B 2 "line3" texto
line1
line2
line3
line4

# 3  
Old 10-20-2009
Thanks guys. Unfortunately there is no -A and -B option in HP-UX. But I received another response about using awk 'c==2 && /i/{print;exit}/[cf]/{c=c?2:1}c' file, which seems to be a very good option. I will try to understand this and tweak for my requirement. Thanks.

Regards,
Praveen
# 4  
Old 10-20-2009
Quote:
Originally Posted by praveen123
But I received another response about using awk 'c==2 && /i/{print;exit}/[cf]/{c=c?2:1}c' file, which seems to be a very good option.
Nop, that's why I delete my post Smilie
You can try
Code:
awk '/i/ && c==2{print;exit} /f/{c=2;for(i in a)print a[i];print}/c/{c=1}c==1{a[NR]=$0}c==2' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search spaces in 5th column in large file

i have a file having 5 columns with more than million records. And i want to search using UNIX command to find if there are any spaces in 5th column. any please help. (1 Reply)
Discussion started by: sivakumar.p
1 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

4. Shell Programming and Scripting

Search and swap multiple lines in file using Perl

Hi all, I have a vcd file with a bunch of lines containing an array, like this $var wire 1 b a $end $var wire 1 c a $end $var wire 1 d a $end $var wire 1 e a $end $var wire 1 f b $end $var wire 1 g b $end $var wire 1 h b $end $var wire 1 i b $end I want it like this: $var wire 1 e a... (12 Replies)
Discussion started by: veerabahu
12 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Shell Programming and Scripting

How to search for multiple lines and put them into one paragraph?

Dear all, I'm trying to manipulate a data file and putting a certain lines into one paragraph. What am I actually want to do is that search some lines in a data file. These lines begin with "1\1\GINC-" and end with "\\@" or the following two empty lines as shown in blue. A part of the text... (11 Replies)
Discussion started by: liuzhencc
11 Replies

7. Shell Programming and Scripting

Script to search a large file with a list of terms in another file

Hi- I am trying to search a large file with a number of different search terms that are listed one per line in 3 different files. Most importantly I need to be able to do a case insensitive search. I have tried just using egrep -f but it doesn't seam to be able to handle the -i option when... (3 Replies)
Discussion started by: dougzilla
3 Replies

8. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies

9. UNIX for Dummies Questions & Answers

Large file search

How do I search through a file that is so large (over 1GB) and find out exactly on what the exact pattern is found. I've used grep and know the line exist but what it returns is not complete because there are blobs in the data and there are control characters, etc and part of the data may beyond... (2 Replies)
Discussion started by: giannicello
2 Replies
Login or Register to Ask a Question