473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Get the current line and next 5 lines

In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?

TIA,
Larry
Jul 19 '05 #1
6 10932
> In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?

1.

open FILE, "d:/1.html";
while (<FILE>){
last if /$pattern/;
}
my ($curr, $next1, $next2) = <FILE>;
close FILE;

print "$curr$next1$next2";

2.

my ($prev1, $prev2);
open FILE, "filename.txt";
while (<FILE>){
last if /$pattern/;
$prev2 = $prev1;
$prev1 = $_;
}
close FILE;
my $curr = $_;

print "$prev2$prev1$curr";
Jul 19 '05 #2
Larry Doan wrote:
In a shell script or Perl, how do I open a file, find what I'm
looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?


Let's see the code you have so far.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #3
On 21 Oct 2003 03:26:37 -0700, Larry Doan
<do********@hotmail.com> wrote:
In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?

If you have Gnu grep, look at the -A and -B options.

--
Cheops' Law:
Nothing ever gets built on schedule or within budget.
Jul 19 '05 #4
do********@hotmail.com (Larry Doan) wrote in message news:<70**************************@posting.google. com>...
In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?


In Perl I'd use a rolling buffer to get previous lines.

my @buffer;
$#buffer = 2; # 3 line buffer (or whatever)
while (<>) {
push @buffer, $_;
shift @buffer;
next unless defined $buffer[0]; # No-op until buffer full
print @buffer if found_what_I_am_looking_for($_);
}

To get next lines you can either use the above approach (but test the
condition on $buffer[0]) or simply use a counter:

my $counter = 0;
while(<>) {
$counter = 3 if found_what_I_am_looking_for($_);
print if $counter-- > 0;
}

The newsgroup comp.lang.perl does not exist (see FAQ). Please do not
start threads here.
Jul 19 '05 #5
In <comp.unix.shell> Larry Doan <do********@hotmail.com> wrote:
In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?


man grep (-A -B)

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 19 '05 #6
William Park <op**********@yahoo.ca> wrote in message news:<7h*****************@nntp-post.primus.ca>...
In <comp.unix.shell> Larry Doan <do********@hotmail.com> wrote:
In a shell script or Perl, how do I open a file, find what I'm looking then

Case 1: grab that line + the next 2 lines
Case 2: grab that line + the previous 2 lines?


man grep (-A -B)

Thanks all. I have enough info to work on.
Larry
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Trevor Fairchild | last post by:
This is so stupidly simple I can't believe I can't figure it out. I need to have vba code identify what line of the page I am currently on in an MS Word Document. Not which paragraph or...
1
by: Nithi Gurusamy | last post by:
Dear Group: ASPError object gives the current line where error occurred. Is there a way to find out the line where Err.Number became non-zero due to a Server.CreateObject(...) failure? Thanks...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
4
by: Ingmar | last post by:
Hi, when jumping to a definition of a function in VB.Net 2005, Visual Studio does not scroll to place the destination line in the center of the window. So it could be anywhere. On my screen I can...
21
by: Zytan | last post by:
Instead of Ctrl+Y redoing what's stored in 'future history', it REPLACES that information (which will then be forever lost) with a new command: delete current line. This shortcut is so ridiculous...
2
by: Chris Lasher | last post by:
Hi all, Using the pdb shell, after repeatedly entering 'l' (lowercase 'L'), how do I jump back to listing the current line again if I don't remember exactly what line my current line is? Do I...
0
by: Rohit111111 | last post by:
Hi, I'm trying to get the current Line Number from the Stack with stackFrame.GetFileLineNumber, how ever the method return 0. What's my error? Thanks in advance for any advice
1
by: hardcoreUFO | last post by:
I have some code that I am trying to debug (Python 2.5.2 on OSX) using pdb. However, when the code reaches the pdb.set_trace(), it does not allow me to view the current line: (Pdb) n (Pdb) l ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.