should be simple..but eh Can you help

B

Bob

Hi...

I have a text file that looks like this:


title: fdsfdsfsd fdsfdsf
number: 234234234234324243
animal: pig

title: sf32erweff fwef wef we
number: 456456456
animal: hen

title: cvbvcxvcxvcvvc
number: 340320324032403
animal: horse

....
...
..


I want to 'grep' for a string whose line begins with title (^title) and
if found
automatically print out that line AND the following 2 lines.

I don't want to use SED, but PERL.

Anyone of you gurus able to assist on how to do this in PERL?

thanks
Bob
 
T

Tore Aursand

Bob said:
I have a text file that looks like this:
[...]
I want to 'grep' for a string whose line begins with title (^title) and
if found automatically print out that line AND the following 2 lines.

What have you tried so far?
I don't want to use SED, but PERL.

It's "sed" and "Perl".
 
A

A. Sinan Unur

Hi...

I have a text file that looks like this:


title: fdsfdsfsd fdsfdsf
number: 234234234234324243
animal: pig

title: sf32erweff fwef wef we
number: 456456456
animal: hen

title: cvbvcxvcxvcvvc
number: 340320324032403
animal: horse

...
..
.


I want to 'grep' for a string whose line begins with title (^title) and
if found
automatically print out that line AND the following 2 lines.

I don't want to use SED, but PERL.

That's why you are posting to comp.lang.perl.misc.

I think the functions index and readline would be useful for your
purpose.

perldoc -f index
perldoc -f readline

By the way, minor but important point: The name of the language is Perl
not PERL.

Sinan.
 
P

Paul Lalli

Bob said:
Hi...

I have a text file that looks like this:


title: fdsfdsfsd fdsfdsf
number: 234234234234324243
animal: pig

title: sf32erweff fwef wef we
number: 456456456
animal: hen

title: cvbvcxvcxvcvvc
number: 340320324032403
animal: horse

I want to 'grep' for a string whose line begins with title (^title) and
if found
automatically print out that line AND the following 2 lines.

I don't want to use SED, but PERL.

Anyone of you gurus able to assist on how to do this in PERL?

First, please read:
perldoc -q difference
Thank you.

Second, is this really what your file looks like? Does it contain
nothing but a series of the three lines you want, each 'record'
separated by a blank line? In that case, just set the input record
separator to paragraph mode:

{
open my $file, '<', 'file.txt' or die "Cannot open file: $!";
local $/ = "\n\n";
while (<$file>){
# $_ now contains your entire record
}
}

If your file does contain other lines you don't want, you're likely
better off using the 'flipflop' operator: ..

open my $file, '<', 'file.txt' or die "Cannot open file: $!";
while (<$file>){
print if /^title:/ .. /^animal:/;
}

The flipflop operator is something at which I usually don't have a great
deal of success explaining well, so instead I'll point you at the
documentation: perldoc perlop (search for 'flip-flop')

Hope this helps
Paul Lalli
 
G

Gunnar Hjalmarsson

[ Please choose a subject line that reflects the subject of your post. ]
I have a text file that looks like this:

title: fdsfdsfsd fdsfdsf
number: 234234234234324243
animal: pig

title: sf32erweff fwef wef we
number: 456456456
animal: hen

title: cvbvcxvcxvcvvc
number: 340320324032403
animal: horse

I want to 'grep' for a string whose line begins with title (^title) and
if found automatically print out that line AND the following 2 lines.

So, what have you tried, and what difficulties have you encountered?
Please study the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

There are many ways to do what you want; this is one:

use Tie::File;
tie my @file, 'Tie::File', 'myfile.txt' or die $!;
print map { join("\n", @file[$_..$_+2]), "\n\n" }
grep substr($file[$_], 0, 5) eq 'title', 0..$#file;
untie @file;
I don't want to use SED, but PERL.

Since you posted here, most people would have guessed so. Why did you
find it worth mentioning?
 
B

Bob

Ok thanks to all of you. You are awesome and I am really taking a
liking to this language..

I opted for the solution to set my input seperator to "\n\n" and it
works like a charm!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top