Elsif not working in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Elsif not working in perl
# 1  
Old 11-07-2014
Elsif not working in perl

have issue where my elsif is always failing. Basically i have a file with sets of 2 lines, the 1st line that containing "ipwr" and the 2nd line containing a value or "unknown". if the 2nd line contains a value then i want to print the pair of lines.

Code:
    open (INFO, "temp.txt") or die;
    while (<INFO>) {
      if ($_ =~ /ipwr/) {
             $pline = $_;
      }elsif ($_ !=~ /unknown/) {
             print "$pline";
             print "$_";
      }
    }
    close INFO;


Last edited by Don Cragun; 11-07-2014 at 10:12 PM.. Reason: Add CODE tags.
# 2  
Old 11-08-2014
If that doesn't do it, post a representative portion of your temp.txt

Code:
open my $fh, "<", "temp.txt" or die "Could not open temp.txt: $!\n";

my $previous_line = "";
while ( my $current_line = <$fh> ) {
    if ( $previous_line =~ /ipwr/ and $current_line !~ /unknown/ ) {
        print "$previous_line $current_line";
    }
    $previous_line = $current_line;
}
close $fh;

# 3  
Old 11-08-2014
2 corrections...
1. open file for reading
2. doesn't match condition
Code:
    open (INFO, "<temp.txt") or die;
    while (<INFO>) {
      if ($_ =~ /ipwr/) {
             $pline = $_;
      }elsif ($_ !~ /unknown/) {
             print "$pline";
             print "$_";
      }
    }
    close INFO;

# 4  
Old 11-08-2014
Both forms are equivalent; opening a file for reading:
Code:
open (INFO, "<temp.txt") or die;
open (INFO, "temp.txt") or die;

However, it is best if no bareword (i.e. INFO) is use for filehandle, since it is global. Instead use a lexical filehandle as I show in the previous post. Also, it is considerate best practice to separate the `<' from the filename string, specially if there's going to be variable interpolation (no interpolation in this case).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Net::IP not working

Experts - I have a snippet of code I can't figure out. I was hoping someone could help me here. I have a file of IPv6 address that I need to format correct. Example in: 2620:0:2d0:200::7 2620:0:2d0:200:a0:c 2620:0a:3f0:200:a0:c I need to convert them to:... (3 Replies)
Discussion started by: timj123
3 Replies

2. Shell Programming and Scripting

IF-THEN-ELSE in PERL not working

Guys, i was trying a simple if-then-else statement in perl; but not getting any success in that. can you please help, where i am wrong. I tried $diff variable with double quotes as well, but no go. $region = $ARGV; $diff = $ARGV; if ; then ($date) = split(' ', `ssh -xC $san cat... (2 Replies)
Discussion started by: sdosanjh
2 Replies

3. Shell Programming and Scripting

using variables in perl not working

sdcprd@dotstoas110:$ echo $F2 1327332411 this works ---------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (1327332411))' 012312 <<<< correct date this doesnt ----------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime... (10 Replies)
Discussion started by: aliyesami
10 Replies

4. Shell Programming and Scripting

perl if else if loop not working

I am trying to work with a text file which has following format and trying ti run if elseif loop but fails. Any help is appreciated 289B ship N-grp 123 289C ship N-grp 123 289D ship N-grp 123 2CE1 flight N-grp 123 2CE2 flight N-grp 123 2CE3 flight N-grp 123 2CE4 flight N-grp 123 2DAF... (4 Replies)
Discussion started by: dynamax
4 Replies

5. Shell Programming and Scripting

\K in perl not working

Hi All, I have just started learning perl and was working on my one-liners tips and tricks. Instead of using the below command : perl -lape 's/(^From:).*/$1 Nelson Elhage <nelhage\@ksplice.com>/' i tried using the \k command using the below command but it gave no results: perl -lape... (2 Replies)
Discussion started by: kunwar
2 Replies

6. UNIX for Dummies Questions & Answers

Perl script not working

Hi Experts!! I have written a very simple script in perl.The script is : $ cat 1.pl #!/usr/bin/perl print "Hi there!\n"; When i ran the above perl script it is showing the following error: $ perl 1.pl -ksh: cd: bad substitution Can anybody,help on this ....as why this script is... (1 Reply)
Discussion started by: Amey Joshi
1 Replies

7. Shell Programming and Scripting

Method isSuccess not working right perl

Good morning all.... I have been learning Perl for about 2 months now and I guess I am getting there as much as I can however I am really stuck. I have a Perl script called postEvent.pl which uses a package called event.pm. PostEvent.pl depends on a meithod inside event.pm called isSuccess to... (0 Replies)
Discussion started by: LRoberts
0 Replies

8. Shell Programming and Scripting

perl if elsif statements

I have having problems with an IF statement in my perl script: if ($model eq "N\\A") {} elsif ($kernel =~ m/xen/) { $model = ("Virtual Machine\n")}; What i am trying to accomplish is if the model is set to "N\A" and the kernel variable has xen somewhere in it i would like to change... (3 Replies)
Discussion started by: insania
3 Replies

9. Shell Programming and Scripting

working with other programs in perl

Hi, I'm still new to perl, and I'm trying to figure out how to work with data output from another program. For example, from a command line I can run "foo -xyz" and it will produce the output I am looking for, which is several lines of text that I will then parse and manipulate within the perl... (6 Replies)
Discussion started by: Decoy
6 Replies

10. Shell Programming and Scripting

Bourne Shell: if elsif question

Hi All, Must be something obvious I am missing, but the simple script below doesn't work. #!/bin/sh x=4 if then echo "x is $x" elsif then echo "x is greater than 4" else echo "x is less than 4" fi When I run this script, I get the error message: 7: Syntax error... (3 Replies)
Discussion started by: leostar_10
3 Replies
Login or Register to Ask a Question