Solved

Perl Script

Posted on 2012-04-03
6
316 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
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: 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 500 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

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!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

739 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