Solved

Converting one liner to a perl script

Posted on 2011-03-25
8
428 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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 …

758 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

21 Experts available now in Live!

Get 1:1 Help Now