Solved

Perl Script

Posted on 2012-04-03
6
312 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
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 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

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 192
syslog unix file 20 68
Perl modules on linux ec2 3 96
How to translate this 2-line while loop into C from Perl? 8 77
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

22 Experts available now in Live!

Get 1:1 Help Now