Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting one liner to a perl script

Posted on 2011-03-25
8
Medium Priority
?
439 Views
Last Modified: 2013-11-13
my $start=10;

command->     perl -i.old -ne 'print unless $start .. $start+10' /etc/postfix/test
0
Comment
Question by:pehalwaan
  • 4
  • 4
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 35219858
#!/usr/bin/perl
use constant start=>10;
@ARGV=('/etc/postfix/test');
$^I='.old';
while( <> ){
  print unless start .. start+10
}
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 35222860
Below is my perl script which finds line number of a pattern in file, stores the line number in a variable and then deletes the 10 lines from the file starting from the variable value. The script works absolutely fine except the problem that it deletes all the lines of file.
I just want to delete the 10 lines starting from line number of pattern.  
#!/usr/bin/perl

# you should always use strict and warnings...
use strict;
use warnings;

my $start;
open IN, '/etc/postfix/test' or die "could not open /etc/postfix/test: $!";
while (<IN>) {
    next unless /zone "abhi.com" {/;
    $start = $.;
    last;
}
close IN;
print $start;

@ARGV=('/etc/postfix/test');
$^I='.old';
while( <> ){
  print unless $start+1 .. $start+10
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 35223604
#!/usr/bin/perl

# you should always use strict and warnings...
use strict;
use warnings;

@ARGV=('/etc/postfix/test');
$^I='.old';
while( <> ){
  print unless ("0".(/zone "abhi.com" {/ .. 0)-2) =~ /^\d$/
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:pehalwaan
ID: 35227057
Hello ozo,
Thanks a lot for providing the script. Also can you also please explain me that how this script works.I want to know the functioning of this script. i.e how it counts the line number of pattern and deletes the lines.
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 35227799
perldoc perlop

       In scalar context, ".." returns a boolean value.  The operator is
       bistable, like a flip-flop, and emulates the line-range (comma)
       operator of sed, awk, and various editors.  Each ".." operator
       maintains its own boolean state.  It is false as long as its left
       operand is false.  Once the left operand is true, the range operator
       stays true until the right operand is true, AFTER which the range
       operator becomes false again.  It doesn't become false till the next
       time the range operator is evaluated.  It can test the right operand
       and become false on the same evaluation it became true (as in awk), but
       it still returns true once.  If you don't want it to test the right
       operand till the next evaluation, as in sed, just use three dots
       ("...") instead of two.  In all other regards, "..." behaves just like
       ".." does.

       The right operand is not evaluated while the operator is in the "false"
       state, and the left operand is not evaluated while the operator is in
       the "true" state.  The precedence is a little lower than || and &&.
       The value returned is either the empty string for false, or a sequence
       number (beginning with 1) for true.  The sequence number is reset for
       each range encountered.  The final sequence number in a range has the
       string "E0" appended to it, which doesn't affect its numeric value, but
       gives you something to search for if you want to exclude the endpoint.
       You can exclude the beginning point by waiting for the sequence number
       to be greater than 1.

       If either operand of scalar ".." is a constant expression, that operand
       is considered true if it is equal ("==") to the current input line
       number (the $. variable).


so
(/zone "abhi.com" {/ .. 0)
returns 1 when it sees a matching line, 2 on the next line, 3 on the line after that, etc.
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 35229666
Hello ozo,

Thanks for the concept. I read the documentation. I am still little confused with the concept.
Can you please tell me the step by step execution of the script so that i can understand it completely.
ie how the values get assigned and how the lines get deleted. Thanks once again for all the help.
0
 
LVL 85

Expert Comment

by:ozo
ID: 35230260
#it may be clearer to write the line as
print unless $count=(/zone "abhi.com" {/ .. 0) and 1 < $count && $count <= 10;
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 35236827
Hello ozo,

Little more help required. I am trying to do same thing now on a remote file. i.e i want to delete lines from a remote file. below is the script. Can you please have a lokk in this why it's not working.
#!/usr/bin/perl

use File::Remote;
my $remote = new File::Remote;
my $count;

my $file="192.168.197.20:/home/file";
$^I='.old';
   $remote->open(FILE, "$file") or die $!;
   print unless $count=(/zone "abhi.com" {/ .. 0) and 1 < $count && $count <= 10;
   $remote->close(FILE);

 
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Simple Linear Regression
Starting up a Project

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question