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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing blank lines 1

Status
Not open for further replies.

gringomike

Technical User
Aug 6, 2003
148
GB
Hi all,

I have perl script which generates output in html format but need to remove every blank line in the file.

Alternatively, I need to remove the first 42 lines from the file.

Either way will work but nothing I've tried has been successful.

Help!

GM
 
If you show us the code that generates the HTML, we could suggest a change to that - that would be preferable to processing the file again after it's been produced.
 
Usually something like
Code:
unless /^\s.$/ {
   # do stuff
}
will skip any lines that are either empty or contain just whitespace.

But ish is right, it's easier to remove them at source than work around them afterwards...
 
Thanks for the replies!

My code is as follows;

#!/bin/ksh
#
# variables for temporary files
#
VFILE=/web/logs/virtualhosts.html
#VFILE=/tmp/virtualhosts.html
VFILE2=/tmp/virtualhostfile
HTTPFILE=/usr/IBMIHS/conf/httpd.conf
#HTTPFILE=httpd.conf

# Use perl to grab the lines in the virual host definitions

perl -ne 'printf if (m#<VirtualHost#i .. m#</VirtualHost>#i) | (m#NameV#i)' $HTTPFILE | grep -v "#" > $VFILE
#
# Get rid of <> around virtual host and other lines
# as this is interpreted by the browser.
# Make the Virtual host lines bold
# This is done by subtly changing similar regular expressions
# and moving the output between two temporary files
#
perl -wpe 's/VirtualHost>/VirtualHost>\n/g' $VFILE > $VFILE2
perl -wpe 's/</(/g' $VFILE2 > $VFILE
perl -wpe 's/>/)/g' $VFILE > $VFILE2
perl -wpe 's/\(/<b>\(/g' $VFILE2 > $VFILE
perl -wpe 's/\)/\)<\/b>/g' $VFILE > $VFILE2


#
# Insert an HTML break at the end of every line
#
# perl -wpe 's/$/\<br>/m' $VFILE2 > $VFILE
cat $VFILE > $VFILE2
#
echo "<b> Virtual host Info for server: `uname -n` date `date`</b>" >$VFILE
echo "<table width="100%" border="1" align="left">" >> $VFILE
echo "<td>" >> $VFILE
cat $VFILE2 >> $VFILE
echo "</td>" >> $VFILE
echo "</table>" >> $VFILE


Cheers!

GM
 
I suspect this can be done a lot more cleanly and easily by doing it all in perl.
Code:
use strict;
use warnings;

my @lines;

while (<DATA>) {
    chomp;
    next if /^\s*(#.*)?$/;
    push @lines, $_ if (m#<VirtualHost#i .. m#</VirtualHost>#i);
}

map {$_ =~ s/</<b>(/g} @lines;
map {$_ =~ s/>/)<\/b>/g} @lines;

print "<html>\n<body>\n<b> Virtual host Info for server: uname -n date date</b>\n";
print qq{<table width="100%" border="1" align="left">\n<TD>\n};
print join("<br/>\n", @lines), "<br/>\n</body>";

__DATA__
junk
<VirtualHost 10.1.2.3>
ServerAdmin webmaster@host.foo.com
DocumentRoot /[URL unfurl="true"]www/docs/host.foo.com[/URL]
#ServerName host.foo.com
ErrorLog logs/host.foo.com-error_log
TransferLog logs/host.foo.com-access_log
</VirtualHost> 


<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /[URL unfurl="true"]www/docs/host.foo.com[/URL]
ServerName host.foo.com
ServerAlias host
</VirtualHost> 

NameV

stuff we don't want
does mostly what you want. You might have to tweak the HTML a bit, it's not my strong point.

I didn't know about the special behaviour of .. between two regexes - I like it, very cool and perlish.

I didn't understand what the | (m#NameV#i) was supposed to do, so I left it out.

The data is based on an Apache 1.3 httpd.conf, which is what IBM use, I think.
 
Forgot to mention, I had to leave out the `uname` and `date` bits, as I'm currently on win32..
 
Just to add my 2c worth. I had to create a list of servers (Win32) using "net view" and then use that list in a Perl script. But net view tends to add the \\ at the start of the names and then whitespaces and sometimes text (server descriptions) after the name. I knocked up this little scipt to deal with it.

Code:
# Open the input file.
open (SRV, "c:\\serverlist.txt") ||
	die "Sorry, file not found.";

# Open the output file. Notice the > this indicates the file can be written to.
open (OUT, ">c:\\newlist.txt") ||
	die "Sorry, can't open this file.";

# Main loop.
while (<SRV>) {

# Removes trailing new line chars.
	chomp;

# Regex to remove Windows backslash's \\
	s/\\\\/\n/;

# Regex to remove all text after the first space found on each line of text.
	s/(\s[ ].+)//;

# Send output to the output file.
	print OUT $_;
}
close (SRV) || die "Can't close file.";

It may not be pretty, but it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top