![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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: . |
|
||||
|
Quote:
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;
}
};
|
|
||||
|
Quote:
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);
Code:
if (/\btotal\b/) {
Code:
if (/\btotal\b/i) {
|
|
||||
|
Quote:
Code:
line: while (<>) {
if (/line 5/){ $c = 5; next line; }
print $_ if $c-- >0 ;
}
|
![]() |
| Bookmarks |
| Tags |
| linux, perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|