Perl Multiple If Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Multiple If Help
# 1  
Old 05-03-2010
Perl Multiple If Help

Hi Friends, i had a tough time in picking up all matching patterns. Additional lines are also picked up. Please help. The below is the input file from which a pattern is matched.

Code:
###Input File  Begin #####
-----[deleted 50 after 49]-----
<   public static final String EMC_EMAIL_SEND_TEST_BUTTON = "button.emcEmailSendTest";
-----[after 70 inserted 70-74]-----
>   public static final String EMPTY_MODEM_TIMEOUT = "advancedCallHomeDialog.editCallHomeCenterDialog.modemTimeout.text";
>   public static final String EMPTY_MODEM_RETRYINERVAL = "advancedCallHomeDialog.editCallHomeCenterDialog.modemRetryInterval.text";
>   public static final String EMPTY_MODEM_MAXRETRIES = "advancedCallHomeDialog.editCallHomeCenterDialog.modemMaxRetries.text";
>   
>   
-----[after 104 inserted 109]-----
>   public static final String OSEM_PORT_NUMBER = Xul.getString("advancedCallHomeDialog.configView.osemPort.text");
-----[deleted 117 after 121]-----
<   public static final String emcEmailPanelStr = Xul.getString("advancedCallHomeDialog.configView.emcEmailPanel.text");
-----[120 changed to 124-131]-----
< 
---
>   public static final String ssllabel = Xul.getString("advancedCallHomeDialog.configView.ssl.text");
>   public static final String timeoutInterval = Xul.getString("advancedCallHomeDialog.timeoutInterval.text");
>   public static final String retryInterval = Xul.getString("advancedCallHomeDialog.retryInterval.text");
>   public static final String maxRetries = Xul.getString("advancedCallHomeDialog.maxRetries.text");
>   public static final String min = Xul.getString("advancedCallHomeDialog.min.text");
>   public static final String sec = Xul.getString("advancedCallHomeDialog.sec.text");
>   
###Input File  END #####

Code:
##### My Script Begin ######
 
foreach my $line1 (@wholeThing)
{
 if($line1=~m/inserted/) 
 {
 print $line1;
  print $line1;
  $insflag=1;
  next;
 }
 if($insflag eq 1)
 { 
 if($line1=~/^>/)
 {
 print "LINE $line1";
  if($line1=~/^>\s+$/)
  {
          $insspace++;
  }
  elsif($line1=~/^<\s+$/)
  {
          $insspace++;
  }
      elsif($line1=~/\/\*.*\*\//)
      {
          $inscmd++;
      }
  elsif($line1=~/\/\//)
  {
          $inscmd++;
      }
      elsif($line1=~/\/\*/)
      {
          $inscmd++;
          $insind=1;
      }
      elsif($line1=~/\*\//)
      {
          $inscmd++;
          $insind=0;
      }
      elsif($line1=~/\*./)
      {
          $inscmd++;
          $insind=0;
      }
      elsif($line1=~/\#./)
      {
          $inscmd++;
          $insind=0;
      }  
      elsif($insind==1)
      {
          $inscmd++;
      }
      else
      {
          $inscode++;
   print $inscode;
   print $line1;
      }
 
 }
 }
 }
$insflag=0;
print "Insert Commented Lines : $inscmd\n";
print "Insert Code Lines : $inscode \n";
print "Insert Empty Lines : $insspace \n"; 
 
#### My Script End #######

The output should be like it matches "inserted" and counts all the lines that follow the match but my script is picking the lines which follow under pattern "changed". Insert has only 4 lines(marked in red) but it is picking 6 lines additionally following under pattern changed and showing output as Insert Code Lines : 10. But it actually is 4. Please help.

Last edited by pludi; 05-03-2010 at 06:41 AM.. Reason: code tags, please...
# 2  
Old 05-03-2010
Quote:
Originally Posted by nmattam
...The output should be like it matches "inserted" and counts all the lines that follow the match but my script is picking the lines which follow under pattern "changed". Insert has only 4 lines(marked in red) but it is picking 6 lines additionally following under pattern changed and showing output as Insert Code Lines : 10. But it actually is 4. ...
Maybe something like this ?

Code:
$ 
$ 
$ cat f8
-----[deleted 50 after 49]-----
<   public static final String EMC_EMAIL_SEND_TEST_BUTTON = "button.emcEmailSendTest";
-----[after 70 inserted 70-74]-----
>   public static final String EMPTY_MODEM_TIMEOUT = "advancedCallHomeDialog.editCallHomeCenterDialog.modemTimeout.text";
>   public static final String EMPTY_MODEM_RETRYINERVAL = "advancedCallHomeDialog.editCallHomeCenterDialog.modemRetryInterval.text";
>   public static final String EMPTY_MODEM_MAXRETRIES = "advancedCallHomeDialog.editCallHomeCenterDialog.modemMaxRetries.text";
>   
>   
-----[after 104 inserted 109]-----
>   public static final String OSEM_PORT_NUMBER = Xul.getString("advancedCallHomeDialog.configView.osemPort.text");
-----[deleted 117 after 121]-----
<   public static final String emcEmailPanelStr = Xul.getString("advancedCallHomeDialog.configView.emcEmailPanel.text");
-----[120 changed to 124-131]-----
< 
---
>   public static final String ssllabel = Xul.getString("advancedCallHomeDialog.configView.ssl.text");
>   public static final String timeoutInterval = Xul.getString("advancedCallHomeDialog.timeoutInterval.text");
>   public static final String retryInterval = Xul.getString("advancedCallHomeDialog.retryInterval.text");
>   public static final String maxRetries = Xul.getString("advancedCallHomeDialog.maxRetries.text");
>   public static final String min = Xul.getString("advancedCallHomeDialog.min.text");
>   public static final String sec = Xul.getString("advancedCallHomeDialog.sec.text");
>   
$ 
$ 
$ ##
$ perl -lne 'if (/^-+\[.*inserted.*/) {$in=1}
>            elsif (/^-+\[.*[^inserted].*/) {$in=0}
>            elsif (!/^[><]\s*$/ && $in==1) {$count++}
>            END {print "Insert Code Lines = $count"}
>           ' f8
Insert Code Lines = 4
$ 
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl multiple qr assigned to variable

Experts, I'm having problems with the code below. I'm trying to test $var2 for two different regexs. I thought it could be done per below, but I'm getting the following error when running. $ ./test.pl b fed50c0100**** Unescaped left brace in regex is deprecated, passed through in regex; marked... (2 Replies)
Discussion started by: timj123
2 Replies

2. Shell Programming and Scripting

Perl Matching multiple variables

I am trying to match mulitple (3) variables. I found the sub given below on the web which works well when all vars are defined. But there are situations where one or two will not be defined (at least one will always be defined.) Example of the variable content possibilities The sub found... (4 Replies)
Discussion started by: popeye
4 Replies

3. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

4. Programming

Perl:how to find multiple strings

Hi , i'm trying to find lines in a file that have 2 matches,but i have trouble with regex -it doesn't search for a second string-please help #!/usr/bin/perl -w use strict; use warnings; my $log = "log"; my $log_string = "grep '^8=' $log |"; usage (); chomp (my $TAB_1 = $ARGV); chomp (my... (2 Replies)
Discussion started by: auto_w
2 Replies

5. Programming

Parser - multiple in Perl

Dear Perl Experts, Could some body help me to find the solution for my problem below: Input file: ----------- THE-0 tsjp THE-32 tsjp THE-64 tsjp Output desired: --------------- THE-0&&-31 tsjp THE-32&&-63 tsjp THE-64&&-95 tsjp Note: 31 = 0+31, (2 Replies)
Discussion started by: askari
2 Replies

6. Shell Programming and Scripting

Multiple lines into one using PERL or SHELL

Hi All, I need your help to solve problem using either PERL script or SHELL script. We are receving a file, in which one record is coming in multiple rows. The main problem is, we are not able to differenciate when the 1st record ends and where the second record starts. For example, ... (4 Replies)
Discussion started by: Amit.Sagpariya
4 Replies

7. Shell Programming and Scripting

Multiple jobs in perl

Hello, I would like to write a perl script which executes several jobs. The key thing is I only want 4 jobs to be executed at one time (that's because my machine as 4 cpu, and I want one job per cpu). Is there any way that I can get perl to co-operate with me in this? Thanks! (1 Reply)
Discussion started by: amcrisan
1 Replies

8. UNIX for Dummies Questions & Answers

Perl Substition with multiple conditions

I have a text file that contains lines similar to the following: ----------------------------------------------------------- fall for setup CSHRC0 'gnd'; CSHR0 'gnd'; rise for setup rise for hold CSHRC0 'gnd'; CSHR0 'gnd'; fall for hold ... (4 Replies)
Discussion started by: EDALBNUG
4 Replies

9. Shell Programming and Scripting

Perl: FH and multiple writes

I found this logging subroutine on the net that I want to use but despite trying many things, I cannot figure out how to get the date in front of the logged text. Ideally what I'm looking for is a line that looks something like this: Wed Aug 20 18:17:29 PDT 2008 - my logging info here. my... (2 Replies)
Discussion started by: gctaylor
2 Replies

10. HP-UX

Multiple Perl installations on HP-UX

This will undoubtedly seem like a problem that should be easily resolved but... We are having some 'issues' getting multiple versions of Perl installed on our HP-UX servers (11.11 & 11.23). Now, I'm not a Sys Admin but I believe the reason behind this is that the Perl installation which comes... (2 Replies)
Discussion started by: Macer
2 Replies
Login or Register to Ask a Question