Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: if or die!

by Corion (Patriarch)
on Mar 01, 2008 at 16:41 UTC ( [id://671391]=note: print w/replies, xml ) Need Help??


in reply to if or die!

Maybe the answers in I just want IF, OR, OR not every one or if elsif elsif prints all three. can help you?

At least show the courtesy to link to your older nodes if you're going to make a new toplevel node about the same problem. You could also spend some more time at explaining what you want your program to do. In separate, short but still coherent sentences. Maybe. Instead. Of using. Punctuation where. *** It does not make. Sense.

Also, providing some sample input and output makes it easier for us to see where your problem is. Also, maybe even provide the output you want as well - this gives us even more to work with.

As to your problem, I don't understand what you're trying to do. But the following line makes no sense:

($line =~ m/'syslogd'\)/);

What is this line supposed to do? Why is it in your program?

Looking further, the whole block makes no sense to me:

} else { ($line =~ m/'syslogd'\)/); my $newline = $. + 3; $newline = $bgs; print OUT $newline; next; }

What is it supposed to do? How does it differ from the following?

print OUT $bgs; next;

Replies are listed 'Best First'.
Re^2: if or die!
by wcj75019 (Acolyte) on Mar 01, 2008 at 17:33 UTC
    I would gladly link these together. I don't know how to do that. No, the answer wasn't in any of those postings. I did get some things that I added. But, the fundamental piece is still not working.
    else { ($line =~ m/'syslogd'\)/); my $newline = $. + 3; $newline = $bgs; print OUT $newline; next;
    If bgs isn't in the file. else go to syslogd. then after 3 lines. add the here document $bgs. Print to OUT file. I can't get it to work. If bgs isn't there. How do I add $bgs after the syslogd.

      I'm sorry, but I still don't understand you. English is not my first language, maybe it's not your first language either - please try to phrase your problem in longer sentences that do not start with "Else".

      I'm trying to rephrase your problem as I interpret it - please correct me if I get it wrong:

      1. You want to print some stuff to the output file if it does not contain the word "bgs".
      2. You want that output to into the third line after the word "syslogd" (which occurs only once in the file anyway).

      Is that correct?

      To attack this problem, let's just reduce all we do to solving this small part and disregard all surrouding stuff. Once we've solved the small part, we can move towards solving the integration into your big program.

      For simplicity, let's assume we have the following input file:

      This is a test file. Here comes the line we want: 'syslogd') You see it? It was two lines ago. And before this line we want to output the other stuff.

      And from how I interpret your task, this is what you want as output:

      This is a test file. Here comes the line we want: 'syslogd') You see it? It was two lines ago. 'bgs') /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect -q>>\$LOG 2>>\$LOG ;; And before this line we want to output the other stuff.

      See how writing down what you have and what you want makes the problem much clearer?

      Now, on to your code:

      First, the line

      ($line =~ m/'syslogd'\)/);
      makes no sense at all. It just checks whether the current line contains "'syslogd')", but then ignores that knowledge completely.

      The line

      my $newline = $. + 3;

      Only assigns to $newline the number of the current line plus three. It does not advance the current file or anything.

      The lines

      $newline = $bgs; print OUT $newline; next;

      print out

      'bgs') /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect -q>>\$LOG 2>>\$LOG ;;

      immediately, instead of waiting some more lines.

      You could have told us that directly - by displaying what output you get.

      Anyway, take a look at the following program, which reads from the DATA filehandle and see if you understand how it does what it does:

      #!/usr/bin/perl -w use strict; my $bgs = <<'HERE'; 'bgs') /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect -q>>$LOG 2>>$LOG ;; HERE # Read the whole file into memory. This makes things easier # for us. Later we can use Tie::File to treat the file # as an array. my @lines = <DATA>; my $saw_bgs; for my $line (@lines) { if ($line =~ /bgs/) { $saw_bgs++; print "I saw 'bgs' in line >$line<\n"; }; }; if (! $saw_bgs) { print "I did not see 'bgs' at all.\n"; }; open my $out, ">", 'test.out' or die "Couldn't create 'test.out': $!"; # Output our file, adding $bgs if necessary my $print_bgs; # Counter for when to print $bgs for my $line (@lines) { if ($line =~ /'syslogd'\)/) { $print_bgs = 2; # Three lines to go } elsif (defined $print_bgs and $print_bgs > 0) { $print_bgs--; # another line done } elsif (defined $print_bgs and $print_bgs == 0) { print {$out} $bgs; # Yay, we counted three lines, so now we +can output $bgs } else { # Nothing to do, just a normal line }; print {$out} $line; }; __DATA__ This is a test file. Here comes the line we want: 'syslogd') You see it? It was two lines ago. And before this line we want to output the other stuff.
        Danke Schon Corion! I haven't gotten it to work yet. But, you put me on the right track. What can I say! I'm a hillbilly, I live in Texas. I think the rest of y'all talk funny. After I learn to format this thing It won't be just one big long ass line.
        You need to be a webmaster just to post here. I tried a few tags and none worked for just simple paragraphs. I looked at the properties. Still no good. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://671391]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found