Solved

Perl Script

Posted on 2012-04-03
6
315 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
  • 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
Industry Leaders: 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 131
Perl script to parse xml file 2 131
collecting information 2 201
Perl modules on linux ec2 3 123
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…

685 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