Pattern matching in file and then display 10 lines above every time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern matching in file and then display 10 lines above every time
# 15  
Old 10-20-2008
After 9 months the thread is a bit stale but maybe your post will still be helpful.
# 16  
Old 10-20-2008
below sed command is use to print out above 3 lines when match 'pat'.

Code:
sed -n '/pat/ !{
1{
   h
}
1 !{
   H
}
}
/pat/ {
H
x
s/\(.*\)\n\(.*\)\n\(.*\)\n\(.*\)\n\(pat\)/\2 \3 \4/
p
}' filename

# 17  
Old 10-20-2008
what about perl...

What about a simple little perl script????.... the 10 lines above the word "total" will be printed... it will read a file called file.txt.

Code:
#!/usr/local/bin/perl
@array;
open(F, "file.txt") or die "cannot read file";
while(<F>) {
  chomp;
  $my_line = "$_";
 
  if ("$my_line" =~ "total") {
    foreach(@array){
      print "$_\n";
    }
    print "========================================================\n"
  }
  push(@array,$my_line);
  if ("$#array" > "9") {
    shift(@array);
  }
};


Last edited by sethcoop; 10-20-2008 at 11:51 PM.. Reason: .
# 18  
Old 04-08-2009
how to print 10 lines below

Thank you sethcoop.. but can you please tell me how to print 10 lines below the occurance of word "total" please help.....
Thanks


Quote:
Originally Posted by sethcoop
What about a simple little perl script????.... the 10 lines above the word "total" will be printed... it will read a file called file.txt.

Code:
#!/usr/local/bin/perl
@array;
open(F, "file.txt") or die "cannot read file";
while(<F>) {
  chomp;
  $my_line = "$_";
 
  if ("$my_line" =~ "total") {
    foreach(@array){
      print "$_\n";
    }
    print "========================================================\n"
  }
  push(@array,$my_line);
  if ("$#array" > "9") {
    shift(@array);
  }
};

# 19  
Old 04-08-2009
Quote:
Originally Posted by digitalrg
Thank you sethcoop.. but can you please tell me how to print 10 lines below the occurance of word "total" please help.....
Thanks
Here is the code to print the number of lines below your search... you can change the variable $lines to be how many lines you want to print after the word total.

Hope this helps!

Code:
#!/usr/bin/perl
$lines = 10;
$x = $lines;
open(F, "file.txt") or die "cannot read file";
while(<F>) {
  chomp;
  $my_line = "$_";
  if ("$x" < "$lines") {
    print "$my_line\n";
    $x++;
  }
  if ("$my_line" =~ "total") {
    $x = 0;
  }
};

# 20  
Old 04-08-2009
Quote:
Originally Posted by digitalrg
Thank you sethcoop.. but can you please tell me how to print 10 lines below the occurance of word "total" please help.....
Thanks
I know sethcoop is trying to help but his perl code is not well written, here is what you want to do:

Code:
#!/usr/bin/perl
use strict;
use warnings;
my $lines = 10;
open(F, "file.txt") or die "cannot read file";
while(<F>) {
  if (/total/)  {
      print scalar <F> for (1..$lines);
      last;
  }
}
close(F);

Couple of things. If you need to find "total" more than once, remove the "last" command" otherwise it quits after the first match. If "total" is not a substring of another word add the \b anchor to the regexp so you don't get any false substring matches for words like "totals" instead of just "total".

Code:
if (/\btotal\b/)  {

add "i" to the regexp to make it case-insensitive if necessary so "TOTAL" and "total" will match:

Code:
if (/\btotal\b/i)  {

# 21  
Old 04-08-2009
Quote:
Originally Posted by digitalrg
.. but can you please tell me how to print 10 lines below the occurance of word "total" please help.....
Thanks
With awk:

Code:
awk '/total/{c=10;next}c-->0' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display all lines after a matching variable

Hi, I have a file called abc.txt with the following dates 2016-01-27 2016-01-28 2016-01-29 2016-01-30 2016-01-31 2016-02-01 2016-02-02 2016-02-03 I would like to print all lines below if 2016-01-31 is found, excluding that date. I use this command --> sed '1,/2016-01-31/d' abc.txt If... (4 Replies)
Discussion started by: Nagesh_1985
4 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

5. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

8. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

Reading lines in a file matching a pattern

Hi, I need to redirect the lines in a file to a different file if the character starting from 2 to 6 in the line are numerical . Please let me know if anyone have any script to do this. Thanks, Ranjit (4 Replies)
Discussion started by: torenji
4 Replies
Login or Register to Ask a Question