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
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)