Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing lines starting with # from a file

Status
Not open for further replies.

InDenial

Technical User
Aug 1, 2003
191
NL
Hi everyone,

I have certain template files. The files look like this:
Code:
###########
# template name:
# purpose:
# etc...
###########

config line 1 containing a <VARIABLE>
another <VARIABLE1> in a config line
and another one. This time it is <VARIABLE3>
.
.
.
etc...

In my script I have the following code:
Code:
open (DEFAULT, "<$default_template") or die;
undef $/;
$config = <DEFAULT>;
close DEFAULT;

The reason I put everything in a string is to be able to do the following:

Code:
while (my ($config_item,$config_value) = each %config_items){
        $config =~ s/<$config_item>/$config_value/g;
}

This works like a charm. The thing I am not sure of is the following:

This above script will run a couple of times wich results in growing $config. This $config will also have lines starting with # wich I want to remove. Since I now have a string and not an array I am not sure how to remove those lines. Is there a way to just convert the string to an array again?

thanks in advance.

InDenial

 
Hmmm I just answered my own question:

Is there a way to just convert the string to an array again?

@array = split(/\n/,$config);

InDenial

 
Code:
open (DEFAULT, "<$default_template") or die;
$config = join( "", grep { not /^#/ } <DEFAULT> );
close DEFAULT;
No lines starting with a comment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top