Solved

position in a file.

Posted on 1998-08-03
5
152 Views
Last Modified: 2010-03-05
I'd like to know how to manage position in a file.
Ex : say I've this file :
hello
hi
everybody
BEGIN
some string
another one
END
yes
no
foo
BEGIN2
exactly
two lines
END2
bar
foo

Suppose I use IN and OUT as filehandles for input and output.
I want to read my file till BEGIN then print all lines till END
then browse till BEGIN2 and then print exactly the 3 following lines
then stop browsing

What's the most efficient and "perl style" manner to do that???
How can I modify the position of <IN> in my file??
0
Comment
Question by:steph84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1208985
Why do you need to modify the position in your input file?  Why can't you just read it line by line until you see BEGIN, then read and print lines until you see END and so on?

If the file is not too big, you can read the entier file in a string array and then do whatever you want with the elements of the array.  I.e. something like this:

open(IN,"inputfile.txt");
@infile = <IN>;
close(IN);

Now $infile[0] will contain the first string from your file, $infile[1] the second one and so on.  The last string will be $infile[@infile].
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208986
The last string will be $infile[@infile-1]
 or $infile[$#infile] or $infile[-1]
If you know the position you want in the file, you can seek to  it.
otherwise, you could say something like:
open(IN,"inputfile.txt") or die "Can't open inputfile.txt: $!";
while( <IN> ){
   print if /^BEGIN/ .. /^END/;
}
0
 

Author Comment

by:steph84
ID: 1208987
Ok, In fact I was looking for something like the seek command. Could you confirm that $. is the current line number??

Is there a special variable to print current line ($_ doesn't work!) If I have :
while (<IN>) {
print "the_line";
}
does just print; work????


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208988
$. is the number of times <> was executed on the current file handle,
which will be the current line if you've been reading line by line.
 while( <IN> ){
   print;
 }
and
 while( <IN> ){
   print $_;
 }
are equivalent

0
 

Author Comment

by:steph84
ID: 1208989
Ok, I grade the first answer (I've a lot of point and this answer was not bad, even if i knew that!). Then I'll ask a blank question you'll be able to answer.
Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question