search for an expression in a file and print the 3 lines above it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for an expression in a file and print the 3 lines above it
# 1  
Old 05-06-2010
search for an expression in a file and print the 3 lines above it

Hi,

I have a file like this

comment.txt
Code:
1.img
2.img
3.img
OK
x.img
y.img
z.img
not ok
1.img
2.img
3.img
bad
1.img
 2.img
 3.img
 OK

i want to print the 3 lines above if the 4th line dose not contain 'OK'

some thing like the following to get an idea.
awk 'NR%4 !~ /OK/ {print $(NR-3)"\n"$(NR-2)"\n"$(NR-1)}' comment.txt


Thanks
# 2  
Old 05-06-2010
Something like:
Code:
awk '{a[NR]=$0}$1 !~ "[OK|img]"{print a[(NR-3)]}' file

# 3  
Old 05-06-2010
Hi danmero,,


It dint work
# 4  
Old 05-06-2010
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 05-07-2010
If
Code:
egrep -e '(not ok|bad)' -B3  comment.txt | grep -v "\--"

is no option, try this Perl code:
Code:
#!/usr/bin/perl

use strict ;
use warnings ;

my $infile='comment.txt' ;
my $count=0 ;
my @temparr=() ;
my @mainarr=() ;

open(F, $infile) or die "Error: $!\n" ;

   my @arr = <F> ;

close(F) ;

  foreach my $record (@arr) {
  chomp $record ;
  push(@temparr,$record) ;
  $count++ ;

  if ($count eq 4) {
  $count=0 ;
    if ($record !~ /OK/) {
    push(@mainarr,@temparr) ;
    @temparr=() ;
    }
  else { @temparr=() ; }
   }
  }

  foreach my $mainarrrec (@mainarr) {
  chomp $mainarrrec ;
  print "$mainarrrec\n" ;
  }

# 6  
Old 05-07-2010
Try...
Code:
nawk 'NR%4==0&&$0!~s{for(c=b+1;c>1;c--)print r[(NR-c+1)%b]}{r[NR%b]=$0}' b=3 s="OK" file1

# 7  
Old 05-07-2010
Code:
sed -n 'N;N;h;n;/OK/!{g;p;}'


Test run:
Code:
$ cat comment.txt
1.img
2.img
3.img
OK
x.img
y.img
z.img
not ok
1.img
2.img
3.img
bad
1.img
 2.img
 3.img
 OK
$ sed -n 'N;N;h;n;/OK/!{g;p;}' comment.txt
x.img
y.img
z.img
1.img
2.img
3.img

Regards,
Alister

Last edited by alister; 05-07-2010 at 01:59 AM.. Reason: Misread problem. Reversed logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to search every file in a directory and print last few lines

Hi everyone, I need to write a script to search a directory, output the name of a file to an ouput file and print the last few lines of the files to the output file such that I would have something like this: FILE1: LINE LINE LINE FILE2: LINE LINE LINE FILE3: LINE LINE LINE... (2 Replies)
Discussion started by: mojoman
2 Replies

2. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

3. Shell Programming and Scripting

Search words in a line and print next 15 lines.

I have a text file ( basically a log file) and i have 2 words (alpha, beta), Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines. ... (4 Replies)
Discussion started by: kashif.live
4 Replies

4. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

5. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

6. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

7. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

8. UNIX for Dummies Questions & Answers

Print lines between the search pattern

hi, I have a file say x.txt containing xxx 123 bla bla ... you xxx dfk dbf ... me xxx ... ... keeps on.. i need to search for pattern in the line starting xxx in the file. If pattern matched, I need to fetch all the lines till i find next xxx. (17 Replies)
Discussion started by: prsshini
17 Replies

9. UNIX for Dummies Questions & Answers

Can I search columns and print lines?

Hi. First post here. I'm relatively new to UNIX (Solaris8) and writing my first real script, but I seem to have hit a brick wall. The odd thing is that it seems like it should be a simple task....but not with UNIX apparently. I've got a large text file: 14000 lines with 10 columns. I want... (2 Replies)
Discussion started by: Ant1815
2 Replies
Login or Register to Ask a Question