Count Line Numbers

[b][red]This message was edited by mookie_gp at 2002-10-4 0:18:21[/red][/b][hr]
[b][red]This message was edited by mookie_gp at 2002-10-4 0:7:16[/red][/b][hr]
[b][red]This message was edited by mookie_gp at 2002-10-4 0:6:7[/red][/b][hr]
Hi,

This should be pretty easy but I'm a bit stumped. I'm trying to parse a text file for 4 specific strings. This text file has listings like:

sometext
Title: This is the title
URL: http://thisistheurl.com
Description: This is the description
Publisher: This is the publisher
sometext
sometext
Title: This is the title
URL: http://thisistheurl.com
Description: This is the description
Publisher: This is the publisher
and so on.

I wanted my script to go until it finds the first instance of 'Title:'. From there I can increment each line to grab the next three lines. It should loop using the foreach until the end of the text file keeping the 4 data points (title, url, description, publisher) together.

The script is close but it doesn't seem to pull what I want. Any suggestions? Any comments would be super greatly appreciated.

#!/usr/bin/perl

my $text = 'file.txt';
open(INPUT, "< $text") or die "Can't open $text
";

@lines = ;

for($i = 0; $i < @lines; $i++) {

foreach $line (@lines) {

if($line =~ m/TITLE:/) {

print "Title: @lines[$i]
";

print "URL: @lines[$i+1]
";

print "Description: @lines[$i+2]
";

print "Publisher: @lines[$i+3]
";
}
print "
";
}
}
close (INPUT);

Currently I'm just printing to stdout but once i get it nailed down i'll be doing inserts into a db table.





Comments

  • : [b][red]This message was edited by mookie_gp at 2002-10-4 0:18:21[/red][/b][hr]
    : [b][red]This message was edited by mookie_gp at 2002-10-4 0:7:16[/red][/b][hr]
    : [b][red]This message was edited by mookie_gp at 2002-10-4 0:6:7[/red][/b][hr]
    : Hi,
    :
    : This should be pretty easy but I'm a bit stumped. I'm trying to parse a text file for 4 specific strings. This text file has listings like:
    :
    : sometext
    : Title: This is the title
    : URL: http://thisistheurl.com
    : Description: This is the description
    : Publisher: This is the publisher
    : sometext
    : sometext
    : Title: This is the title
    : URL: http://thisistheurl.com
    : Description: This is the description
    : Publisher: This is the publisher
    : and so on.
    :
    : I wanted my script to go until it finds the first instance of 'Title:'. From there I can increment each line to grab the next three lines. It should loop using the foreach until the end of the text file keeping the 4 data points (title, url, description, publisher) together.
    :
    : The script is close but it doesn't seem to pull what I want. Any suggestions? Any comments would be super greatly appreciated.
    :
    : #!/usr/bin/perl
    :
    : my $text = 'file.txt';
    : open(INPUT, "< $text") or die "Can't open $text
    ";
    :
    : @lines = ;
    :
    : for($i = 0; $i < @lines; $i++) {
    :
    : foreach $line (@lines) {
    :
    : if($line =~ m/TITLE:/) {
    :
    : print "Title: @lines[$i]
    ";
    :
    : print "URL: @lines[$i+1]
    ";
    :
    : print "Description: @lines[$i+2]
    ";
    :
    : print "Publisher: @lines[$i+3]
    ";
    : }
    : print "
    ";
    : }
    : }
    : close (INPUT);
    :
    : Currently I'm just printing to stdout but once i get it nailed down i'll be doing inserts into a db table.
    :
    :
    :
    :
    :
    :
    Hello boy
    I think ur problem is about case sensitivity. Look at what I did (sorry for my sad use of the English Language):

    #!/usr/bin/perl

    my $text = 'textfile.txt';
    open(INPUT, "< $text") or die "Can't open $text
    ";

    @lines = ;

    # $flag_esecuzione_matching = 0; i allocated this variable to know if #the process was actually run by the interpreter

    for($i = 0; $i < @lines; $i++) {

    if(@lines[$i] =~ /TITLE:/i) {

    # you'll see there is an "i" after the regex; that stands for ->"case Insensitivity"

    # note also I have deleted all that foreach stuff avoiding the thing to loop for (@lines)^2

    # $flag_esecuzione_matching = 1;

    print "@lines[$i]
    ";
    #note that the text file already include the field name

    print "@lines[$i+1]
    ";

    print "@lines[$i+2]
    ";

    print "@lines[$i+3]
    ";
    }
    }

    #print $flag_esecuzione_matching;
    close (INPUT);


    I'd really like you to read this tutorial http://www.netcat.co.uk/rob/perl/win32perltut.html

    Here you can really learn most of the functionality of the perl w/o getting tired of it, believe me.

    For anything else, you can contact me
  • : : [b][red]This message was edited by mookie_gp at 2002-10-4 0:18:21[/red][/b][hr]
    : : [b][red]This message was edited by mookie_gp at 2002-10-4 0:7:16[/red][/b][hr]
    : : [b][red]This message was edited by mookie_gp at 2002-10-4 0:6:7[/red][/b][hr]
    : : Hi,
    : :
    : : This should be pretty easy but I'm a bit stumped. I'm trying to parse a text file for 4 specific strings. This text file has listings like:
    : :
    : : sometext
    : : Title: This is the title
    : : URL: http://thisistheurl.com
    : : Description: This is the description
    : : Publisher: This is the publisher
    : : sometext
    : : sometext
    : : Title: This is the title
    : : URL: http://thisistheurl.com
    : : Description: This is the description
    : : Publisher: This is the publisher
    : : and so on.
    : :
    : : I wanted my script to go until it finds the first instance of 'Title:'. From there I can increment each line to grab the next three lines. It should loop using the foreach until the end of the text file keeping the 4 data points (title, url, description, publisher) together.
    : :
    : : The script is close but it doesn't seem to pull what I want. Any suggestions? Any comments would be super greatly appreciated.
    : :
    : : #!/usr/bin/perl
    : :
    : : my $text = 'file.txt';
    : : open(INPUT, "< $text") or die "Can't open $text
    ";

    : :
    : : @lines = ;
    : :
    : : for($i = 0; $i < @lines; $i++) {
    : :
    : : foreach $line (@lines) {
    : :
    : : if($line =~ m/TITLE:/) {
    : :
    : : print "Title: @lines[$i]
    ";
    : :
    : : print "URL: @lines[$i+1]
    ";
    : :
    : : print "Description: @lines[$i+2]
    ";
    : :
    : : print "Publisher: @lines[$i+3]
    ";
    : : }
    : : print "
    ";
    : : }
    : : }
    : : close (INPUT);
    : :
    : : Currently I'm just printing to stdout but once i get it nailed down i'll be doing inserts into a db table.
    : :
    : :
    : :
    : :
    : :
    : :
    : Hello boy
    : I think ur problem is about case sensitivity. Look at what I did (sorry for my sad use of the English Language):
    :
    : #!/usr/bin/perl
    :
    : my $text = 'textfile.txt';
    : open(INPUT, "< $text") or die "Can't open $text
    ";
    :
    : @lines = ;
    :
    : # $flag_esecuzione_matching = 0; i allocated this variable to know if #the process was actually run by the interpreter
    :
    : for($i = 0; $i < @lines; $i++) {
    :
    : if(@lines[$i] =~ /TITLE:/i) {
    :
    : # you'll see there is an "i" after the regex; that stands for ->"case Insensitivity"
    :
    : # note also I have deleted all that foreach stuff avoiding the thing to loop for (@lines)^2
    :
    : # $flag_esecuzione_matching = 1;
    :
    : print "@lines[$i]
    ";
    : #note that the text file already include the field name
    :
    : print "@lines[$i+1]
    ";
    :
    : print "@lines[$i+2]
    ";
    :
    : print "@lines[$i+3]
    ";
    : }
    : }
    :
    : #print $flag_esecuzione_matching;
    : close (INPUT);
    :
    :
    : I'd really like you to read this tutorial http://www.netcat.co.uk/rob/perl/win32perltut.html
    :
    : Here you can really learn most of the functionality of the perl w/o getting tired of it, believe me.
    :
    : For anything else, you can contact me
    :


    Hi,

    No, it's not due to case sensitivity. The line is matching fine because it's always: TITLE. Upper case. Thank you though for taking the time to reply. :)
  • : Hi,
    :
    : No, it's not due to case sensitivity. The line is matching fine because it's always: TITLE. Upper case. Thank you though for taking the time to reply. :)

    So, Mookie,I can't yet understand what's the problem about the code (if any). I mean: what would you like your code did for you?
    best regards,
    Stefano

  • : Hi,
    :
    : This should be pretty easy but I'm a bit stumped. I'm trying to parse a text file for 4 specific strings. This text file has listings like:
    :
    : sometext
    : Title: This is the title
    : URL: http://thisistheurl.com
    : Description: This is the description
    : Publisher: This is the publisher
    : sometext
    : sometext
    : Title: This is the title
    : URL: http://thisistheurl.com
    : Description: This is the description
    : Publisher: This is the publisher
    : and so on.
    :
    : I wanted my script to go until it finds the first instance of 'Title:'. From there I can increment each line to grab the next three lines. It should loop using the foreach until the end of the text file keeping the 4 data points (title, url, description, publisher) together.
    :
    : The script is close but it doesn't seem to pull what I want. Any suggestions? Any comments would be super greatly appreciated.
    :
    : #!/usr/bin/perl
    :
    : my $text = 'file.txt';
    : open(INPUT, "< $text") or die "Can't open $text
    ";
    :
    : @lines = ;
    :
    : for($i = 0; $i < @lines; $i++) {
    :
    : foreach $line (@lines) {
    :
    : if($line =~ m/TITLE:/) {
    :
    : print "Title: @lines[$i]
    ";
    :
    : print "URL: @lines[$i+1]
    ";
    :
    : print "Description: @lines[$i+2]
    ";
    :
    : print "Publisher: @lines[$i+3]
    ";
    : }
    : print "
    ";
    : }
    : }
    : close (INPUT);
    :
    : Currently I'm just printing to stdout but once i get it nailed down i'll be doing inserts into a db table.
    :
    :

    Hm... May be I misunderstood what you need, but I have a question
    How many times you want to execute this block:
    : foreach $line (@lines) {
    : }

  • Mookie,

    I think this is a bit closer to what you're trying to do...

    [code]
    #!/usr/bin/perl

    my $text = 'file.txt';
    open(INPUT, "< $text") or die "Can't open $text
    ";
    @lines = ;
    close (INPUT);

    for($i = 0; $i < @lines; $i++) {

    if($lines[$i] =~ m/TITLE:/i) {

    print "Title: $lines[$i]
    ";
    print "URL: $lines[$i+1]
    ";
    print "Description: $lines[$i+2]
    ";
    print "Publisher: $lines[$i+3]
    ";
    print "
    ";
    }
    }
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories