Solved

Converting one liner to a perl script

Posted on 2011-03-25
8
433 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now