Grepping before and after lines for required string


 
Thread Tools Search this Thread
Operating Systems AIX Grepping before and after lines for required string
# 1  
Old 12-14-2012
[SOLVED] Grepping before and after lines for required string

Hi All,

I am new to AIX unix . i need to grep for a pattern and if pattern is found then i need 3 before the pattern line found and 3 lines after the pattern found.
# 2  
Old 12-14-2012
grep greps lines, it can't do logic or other recall, it's not really a programming language. The awk language can do logic and recall.

Code:
$ cat <<"EOF" > context.awk
# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}

{ LINE[(++L)%10]=$0 } # Remember line for later

$0 ~ PATTERN { for(N=3; N>=1; N--) print last(N);
        print;
        for(N=0; N<3; N++) { getline ; print }
}
EOF

$ nawk -f context.awk PATTERN="myregex" filename1 filename2 ...

# 3  
Old 12-14-2012
Hi I have to search this in *.gz file , will it be work?

---------- Post updated at 03:16 PM ---------- Previous update was at 03:14 PM ----------

Quote:
Originally Posted by Corona688
grep greps lines, it can't do logic or other recall, it's not really a programming language. The awk language can do logic and recall.

Code:
$ cat <<"EOF" > context.awk
# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}
 
{ LINE[(++L)%10]=$0 } # Remember line for later
 
$0 ~ PATTERN { for(N=3; N>=1; N--) print last(N);
        print;
        for(N=0; N<3; N++) { getline ; print }
}
EOF
 
$ awk -f context.awk PATTERN="myregex" filename1 filename2 ...

like in GNU unix we have grep -a and grep -b similar to that . will your solution works for .gz files also
# 4  
Old 12-14-2012
GNU unix is a contradiction in terms. GNU means "GNU's not UNIX". Smilie

Nope. Neither will grep. You can still stream it, of course.

Code:
gunzip < filename | nawk -f context.awk PATTERN="myregex"

# 5  
Old 12-14-2012
Slightly improved version:

Code:
cat <<"EOF" > context.awk
# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%CTX]);
}
 
{ LINE[(++L)%10]=$0 } # Remember line for later
 
$0 ~ PATTERN { for(N=CTX; N>=1; N--) print last(N);
        print;
        for(N=0; N<CTX; N++) { getline ; print }
}
EOF

Code:
$ gunzip < filename | awk -f context.awk CTX=3 PATTERN="myregex"

# 6  
Old 12-14-2012
Quote:
Originally Posted by Corona688
GNU unix is a contradiction in terms. GNU means "GNU's not UNIX". Smilie

Nope. Neither will grep. You can still stream it, of course.

Code:
gunzip < filename | nawk -f context.awk PATTERN="myregex"

Thanks a lot this what i need , one more question if i give *.gz to found the pattern in multipile gz file it's not working any idea about that
# 7  
Old 12-14-2012
If you have zcat, you can do zcat *.gz | nawk ...

If you don't, you can do for FILE in *.gz ; do gunzip < "$FILE" ; done | nawk ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Required 3 lines above the file and below file when string matches

i had requirement like i need to get "error" line of above 3 and below 3 from a file .I tried with the below script.But it's not working. y='grep -n -i error /home/file.txt|cut -c1' echo $y head -$y /home/file.txt| tail -3 >tmp.txt tail -$y /home/file.txt head -3 >>tmp.txt (4 Replies)
Discussion started by: bhas85
4 Replies

2. Shell Programming and Scripting

Grepping the logs with respect to string search

Hi Folks, I have a log file at the following location.. /opt/ert/abc.log Now abc.log contain the following enteries in this format below.. 23-Jul-2014 10:09.32.204 ERROR abc.log cdfrer tyre fgty >>>>> cqno : 78539132 abc Id : 0 Sabc : 20140724 Now in log file (abc.log) I want to... (2 Replies)
Discussion started by: tuntun343466
2 Replies

3. Shell Programming and Scripting

Grepping multiple lines in a file

HI I have a file with output as System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 I need to grep the values of following column fields u, s, w and i from each row sum them up and store in a variable..:( Please help.. (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

grepping multiple matches in a single string

Hi All, I'm trying to grep for 3 patterns in a string of gibberish. It so happens that each line is appended by a date/time stamp and i was able to figure out how to extract only the datetime. here is the string.. i have to display tinker tailor soldier spy Please can some help... (2 Replies)
Discussion started by: Irishboy24
2 Replies

6. AIX

Help with looping a file and grepping a string

I have 2 files: fileA and fileB. content of fileA --------------- admin.teacher is in new york; admin.mason is in new york; admin.driver is in new york city; user.trucker is in hartford; admin.developer is in new york state; content of fileB ---------------- admin.teacher is in... (8 Replies)
Discussion started by: lowprofile
8 Replies

7. UNIX for Dummies Questions & Answers

How get only required lines & delete the rest of the lines in file

Hiiii I have a file which contains huge data as a.dat: PDE 1990 1 9 18 51 28.90 24.7500 95.2800 118.0 6.1 0.0 BURMA event name: 010990D time shift: 7.3000 half duration: 5.0000 latitude: 24.4200 longitude: 94.9500 depth: 129.6000 Mrr: ... (7 Replies)
Discussion started by: reva
7 Replies

8. Shell Programming and Scripting

Grepping string from out file

Guys .. Need to pull this highlighted strings irrespective of line numbers & should be echoed . But these strings are from Outfile from different dir. In which way this can be grepped ?? Need an idea http-timeout 120 seconds persistent-timeout 180 seconds host-rewriting on ... (7 Replies)
Discussion started by: raghunsi
7 Replies

9. Shell Programming and Scripting

regular expression grepping lines with VARIOUS number of blanks

Hi, I need a regular expression grepping all lines starting with '*' followed by a VARIOUS number of blanks and then followed by the string 'Runjob=1'. I tried that code, but it doesn't work: grep -i '*'+'Runjob=1' INPUT_FILE >>OUTPUT_FILE Can someone help me? Thanks (8 Replies)
Discussion started by: ABE2202
8 Replies

10. UNIX for Advanced & Expert Users

grepping lines out of files

Hi, I wonder if anyone can help me. I have a file with about 200 lines in it. I am wanting to set up a Count so that it picks out each line at turn and edits the line. However i am having trouble pulling out the specific line. I have a feeling it will be done somehow by a grep -n but what ever i... (2 Replies)
Discussion started by: mariner
2 Replies
Login or Register to Ask a Question