The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 06:37 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 04:15 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 03:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-20-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
After 9 months the thread is a bit stale but maybe your post will still be helpful.
  #2 (permalink)  
Old 10-20-2008
sethcoop sethcoop is offline
Registered User
  
 

Join Date: Oct 2008
Location: United States
Posts: 34
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 10:51 PM.. Reason: .
  #3 (permalink)  
Old 04-08-2009
digitalrg's Avatar
digitalrg digitalrg is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 26
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 View Post
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);
  }
};
  #4 (permalink)  
Old 04-08-2009
sethcoop sethcoop is offline
Registered User
  
 

Join Date: Oct 2008
Location: United States
Posts: 34
Quote:
Originally Posted by digitalrg View Post
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;
  }
};
  #5 (permalink)  
Old 04-08-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by digitalrg View Post
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)  {
  #6 (permalink)  
Old 04-10-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,508
Quote:
Originally Posted by KevinADC View Post
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);
borrowed from awk
Code:
line: while (<>) {
    if (/line 5/){ $c = 5; next line; }
    print $_ if $c-- >0 ;
}
  #7 (permalink)  
Old 04-08-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Quote:
Originally Posted by digitalrg View Post
.. 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
Closed Thread

Bookmarks

Tags
linux, perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0