Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: if or die!

by wcj75019 (Acolyte)
on Mar 01, 2008 at 17:33 UTC ( #671401=note: print w/ replies, xml ) Need Help??


in reply to Re: if or die!
in thread if or die!

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.


Comment on Re^2: if or die!
Download Code
Re^3: if or die!
by Corion (Pope) on Mar 01, 2008 at 18:01 UTC

    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.
Re^3: if or die!
by polettix (Vicar) on Mar 01, 2008 at 22:47 UTC
      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.

        No you don't.

        In fact, your comment

        I tried a few tags and none worked for just simple paragraphs.

        seems to reflect a lack of even a rudimentary understanding of html (and there's not, in this case, any variance between standard 4.01 and the (sometimes) specialized sub- and -superset used here... and easily available to anyone who choses to follow the links in the "Information" nodelet which appears on all (most?) PM pages.

        In other words, you don't "need to be a webmaster;" you just need to make the effort (and have the courtesy) to read the site documents.

        Try Perl Monks Approved HTML tags, the tips at the bottom of each text-entry box, and Writeup Formatting Tips.

        And don't whine about the results of what appears to be your failure to do so.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-07-29 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls