Solved

Converting one liner to a perl script

Posted on 2011-03-25
8
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 84

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 84

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
Independent Software Vendors: 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 84

Accepted Solution

by:
ozo earned 500 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 84

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

752 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