?
Solved

Perl Script

Posted on 2012-04-03
6
Medium Priority
?
318 Views
Last Modified: 2012-04-03
I have a file containing the below data.


define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav.cl.da
alias                   abhinav.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }
define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav1.cl.da
alias                   abhinav1.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }

Now i want to write a script that searches for a pattern "hostname      abhinav.cl.da" and delete the 2 lines above it and 8 lines below it.

I thought of finding  the line number of  pattern and then running  loop and deleting the lines but getting something wrong in the loop. Can anyone write the correct script for me.?
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37802944
perl -0777pe 's/(.*\n){2}hostname\s+abhinav\.cl\.da\n(.*\n){8}//g'
0
 
LVL 84

Expert Comment

by:ozo
ID: 37802966
or, if you want to delete between } instead of counting lines
perl -0175ne 'print unless /hostname\s+abhinav\.cl\.da/'
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 37802971
There are several ways to approach this, one being to set the input record separator to: "                      }\n" and read-in the data in chunks; each chunk being the complete "define host {  } block.

Then use a simple regex to decide which block(s) to keep and which to skip.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:pehalwaan
ID: 37802987
It's not working. Also, i would not prefer a one liner. If you can write a code,that would be really appreciable.
0
 
LVL 3

Author Comment

by:pehalwaan
ID: 37802998
FishMonger-  Can you write the code for me for the approach you are suggesting
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 total points
ID: 37803035
#!/usr/bin/perl

use strict;
use warnings;

$/ = "                      }\n";
my $host = 'abhinav1.cl.da';

while ( <DATA> ) {
    print unless /$host/;
}


__DATA__
define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav.cl.da
alias                   abhinav.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }
define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav1.cl.da
alias                   abhinav1.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }
define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav2.cl.da
alias                   abhinav2.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }
define host              {
use                     passive-linux-check-100-Mbps
hostname                abhinav3.cl.da
alias                   abhinav3.cl.da
_SERVERTYPE             virtual
_IPADDRESS              192.168.201.100
_VMHOSTIP                 192.168.203.200
_VMHOST                   vmware54.dl1.da
check_command           host-is-stale
contact_groups          admins
                      }

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

771 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