Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting one liner to a perl script

Posted on 2011-03-25
8
Medium Priority
?
438 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 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 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

660 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