egrep 4th line from match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep 4th line from match
# 1  
Old 05-10-2010
Data egrep 4th line from match

Can some one help me to print 4th line before the match using egrep or grep command options.
i have a very large file and i need to search the entire file, look for the match (key word) and print 4th line before the matched key word.
# 2  
Old 05-10-2010
Code:
[house@leonov] cat test.file
four
three
two
one
gotcha
[house@leonov] grep -B4 'gotcha' test.file | head -n1
four

# 3  
Old 05-10-2010
Try...
Code:
awk '$0~s{print r[NR%b]}{r[NR%b]=$0}' b=4 s="keyword" file1

# 4  
Old 05-10-2010
Code:
while read line
do

echo $line | grep $pattern >/dev/null



if [ $? -eq 0 ]
then
echo $line4

fi

line4=$line3
line3=$line2
line2=$line1
line1=$line
done < $filename

This User Gave Thanks to amitranjansahu For This Post:
# 5  
Old 05-10-2010
thanks to you all, i am looking single line command and i am getitng error for first two replies.

Code:
grep: illegal option -- B
grep: illegal option -- 4
Usage: grep -hblcnsviw pattern file . . .

-----------------------------------------------------

Code:
ksh: -ksh~s{print:  not found
awk: syntax error near line 1
awk: bailing out near line 1

------------------------------------------------------

Last edited by Scott; 05-10-2010 at 03:07 AM.. Reason: Code tags
# 6  
Old 05-10-2010
Make sure that you use the 'single quotes' around the awk code - as posted.
# 7  
Old 05-10-2010
hi,i tried with that option also. still getting error. below is my exact command.

# awk '$0~s{print r[NR%b]}{r[NR%b]=$0}' b=4 s="0x9f 0x81 0x19 0x01 0x02" test2.log | pg
awk: syntax error near line 1
awk: bailing out near line 1


there are some spaces in the key word am searching, will that matter?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

Egrep - Only Match First Occurrence

echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1}' String#1 String#2 I'm trying to just match the first occurrence of 'String#' + 1 character. I thought the "-m 1" switch would do that for me. Instead I get both occurrences. Can somebody provide some insight? Thanks! (5 Replies)
Discussion started by: sudo
5 Replies

3. Shell Programming and Scripting

Reading in 4th number in a line

I'd like to read a file (mydata.dat) line by line. The file consists of 5 columns filled with numbers, like so: 83.018 1.953 49.587 20550.000 353 83.213 1.953 49.195 20600.000 171 84.935 1.954 48.803 20650.000 920 For every read line (i.e. in every... (2 Replies)
Discussion started by: pina
2 Replies

4. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

5. Shell Programming and Scripting

Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is egrep '^(\\)\*$'No luck. I've searched the web and not much luck. I know about the escape character \ but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks! (8 Replies)
Discussion started by: matthewfs
8 Replies

6. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

7. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

8. UNIX for Dummies Questions & Answers

retrieve every 4th line from a file

Hello, I want to retrieve 2, 6, 10, 14...... (each 4 lines apart) from a file that looks like the sample below. In other words, I want only lines corresponding to the Xs. Header1_a XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Header1_b yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Header2_a... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. Shell Programming and Scripting

How to replace all after 4th character in line ?

How to replace all after 4 character at line? & convert to upper case ? google.com -> GOOG (2 Replies)
Discussion started by: Trump
2 Replies

10. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies
Login or Register to Ask a Question