Perl Script: Deleting a block of text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script: Deleting a block of text
# 1  
Old 07-22-2009
Perl Script: Deleting a block of text

Say I have a file with a bunch of config blocks (see below) in a file. If I send a variable to the function, how can I remove that block of text?

Code:
define host{
   host     abc
   description   testserver
}

define host{
   host     xzy
   description    prodserver
}

So in the example above, I want to remove host name "abc," I want the config block for "abc" host gone from the file.
# 2  
Old 07-22-2009
Hi.

As usual, lots of ways to do this; here's a simple one:
Code:
#!/usr/bin/perl

# @(#) p1	Demonstrate record separator for deleting stanzas.

use warnings;
use strict;

use English qw( -no_match_vars );

my($debug);
$debug = 0;
$debug = 1;

my($sep) = "}";
$INPUT_RECORD_SEPARATOR = $sep;

my($delete) = "abc";

while ( <> ) {
  next if /$delete/;
  print;
}

exit(0);

Assuming your data file is on "data1", this produces:
Code:
% ./p1 data1


define host{
   host     xzy
   description    prodserver
}

See perldoc perldoc for how to find details of operations ... cheers, drl
# 3  
Old 07-22-2009
awesome ... now i can move on w/ the rest of the code!

thx a lot. Question, what does this line do?

Code:
use English qw( -no_match_vars );

# 4  
Old 07-22-2009
Hi.
Code:
perldoc -f use
perldoc English

cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to extract text from image file

Hi Folks, Could you please share your ideas on extracting text from image file(jpg,png and gif formats). Regards, J (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

3. Shell Programming and Scripting

Convert binary to text Perl script

Hello everyone, I have a binary file with a structure unknown. I have found 2 perl scripts that it seems to do the convertion but I get sintactic errors when I run them, may somebody test these 2 scripts please and see if really work? One if from here... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

4. UNIX for Dummies Questions & Answers

Deleting Block of Text from a File

Hi I am looking for the way to delete the block of data for example original file line1 line2 line3 line4 line5 input file line2 line3 original file should contain line1 line4 line5 (3 Replies)
Discussion started by: rakeshkumar
3 Replies

5. Shell Programming and Scripting

Format text to bold from perl script to csv

Hi everyone, is there any way in perl using which we can print the selective words in bold when we write the output to a csv file? Please find the example below 1. Filename: A 2. name age 12 3. city add 23 Line1 should only be bold. Outputs from other files being read in the... (2 Replies)
Discussion started by: ramakanth_burra
2 Replies

6. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

7. Shell Programming and Scripting

URGENT: Script/Function needed to read text property files in block wise

Hi, Iam in a need for a script/function in KSH where I want to read a text file (property file) in block by block. Here is the example: Heading Name Descripton Block Block1 Value1 Description Property Name Value Property Name Value Property Name Value Property Name Value Property Name... (7 Replies)
Discussion started by: ysreenivas
7 Replies

8. Shell Programming and Scripting

csh script for deleting extra spaces in text file

I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Discussion started by: hertingm
2 Replies

9. Shell Programming and Scripting

Perl script to load text file into DB field

Hello, maybe this post is offtopic, sorry for the inconveniencies (maybe should be in the forum with questions about C, C++, Java, SQL...). I found examples about how to populate different fields from a text file (with MySQL, the LOAD DATA INFILE sentece), but not about how to load a complete... (2 Replies)
Discussion started by: aristegui
2 Replies

10. Shell Programming and Scripting

Deleting text block in file

Need to delete a text block inside a file, that is marked with a start and an end pattern. Eg do not delete not delete <tag1> delete everything here here and here and here... <tag2> do not delete do not delete.... Believe sed is able to do this job but don't get it working. ... (1 Reply)
Discussion started by: andre123
1 Replies
Login or Register to Ask a Question