Solved

Perl Script

Posted on 2012-04-03
6
311 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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