Discussion Locked

Scripting - Remove blank line from file

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Can anyone tell me how to remove a blank line from the end of a .txt file using NT shell scripting language.
When i pipe the contents of
for /f "tokens=2 delims=:" %%i in ('ipconfig ^/all ^| find ^/i "ip address"') do echo %%i ^>> c:\temp\ipinfo.txt

I get a square character?? at the end of each IP address and a carriage return at the end of the file.
How can these be avoided or better, removed. I would like to know how to remove these for future reference.

Thanks to the whizz kid whoanswers this.
Stuart

7 replies

by timthetoolman

Hi,

It seems that echo always ends with a carriage return. Would it be acceptable for you to run a conversion afterward on the temporary file produced?

I'm thinking the whole thing would be better done in Perl. Is that an option? Or is there agood reason to be doing it in the limited world of DOS...??

Let me know.

Cheers,
Tim.

by superflex

It depends on what you mean by conversion? If you mean edit and remove the carriage return and blank line then yes.

While I do intend to learn other languages and agree that NT shell scripting may be limited in comparison to others like PERL, I believe that NT shell script will do what I need it to but I just can't find out how its done yet.

by timthetoolman

Hi,

OK, Dont close this question for a couple more days and I'll write you the Perl script do do this. Please let me know if you're not interested in this solution.

Its quite simple - just a few lines of Perl... and I'll comment my code well so you can see what's happenning. Perl is an excellent language and a simple one too - well worth learning.

I just want to clarify one point.

At the end of the day, you want a text file containing a list of the valid IP addresses on the machine,right?

Cheers,
Tim.

by timthetoolman

Oh, One more thing... By "conversion" I meant something like DOS2UNIX which stripts the extra LF off the end of each line in a file.

Cheers,
Tim.

by timthetoolman

Hi,

OK, The following two comments are the code. Copy and paste it into a bat file - its designed to run as a batch file.

If you need Perl itself, then you can download it from

http://www.perl.com/pub/a/language/info/software.html

(remove spaces from the above URL)

I hope this helps you...

@rem = '--*-Perl-*--
@echo off

if not exist %0 set batchf=%0.bat
if exist %0 set batchf=%0

if "%OS%" == "Windows_NT" goto WinNT
perl -x %batchf% %1 %2 %3 %4 %5 %6 %7 %8 %9
if not errorlevel 0 goto fail1
goto endofperl

:WinNT
perl -x %batchf% %*
if not errorlevel 0 goto fail1
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul

:endofperl
echo.
pause
exit

:fail1
echo.
echo Problem with the IP address script.
echo.
goto endofperl

by timthetoolman

#!perl

# The Pattern is the string to check for from the
# ipconfig/all output (note if you wanted to retain that
# extra space at the beginning of the line like in your
# example code, then remove the space after the : below

$pattern = "IP Address. . . . . . . . . . . . : ";

# The CRflag is used to ensure that no CR happens at the
# end of the file. The logic goes... print the IP address
# set the flag. Net time you print an IP address if the
# flag is set, then print a CR first.

$CRflag = "FALSE";

# The command is obviously to create the first text file
# that gets parsed to find the IP address. There may be a
# Perl function to do this directly but I haven't gone
# hunting for it. This works fine. Note you could change
# temp.txt to c:\temp\temp.txt if you wanted.

@command = ("ipconfig/all > temp.txt");
system(@command) == 0 or die "Couldn't find IP information";

# IPList.txt is the file produced containing the IP addresses.

open (FILE1, "< temp.txt") or die "Cant open temporary IP Config file";
open (FILE2, "> IPList.txt") or die "Cant open temporary IP output file";

# This loop reads the file in looking for the pattern above and
# when it finds it, spits the result out to the output file
# The CRflag logic is described above and ensures no final
# carriage return at the end of the file.

while () {
if (/($pattern)(.*)(\n)/) {
if ($CRflag eq "FALSE") {
$CRflag = "TRUE";
} else {
print FILE2 "\n";
} print FILE2 "$2";
}
}

# Close up to finish off.

close FILE1;
close FILE2;

by superflex

Although I wasn't looking for the PERL answer I do appreciate the detailed PERL solution so I would like to donate 100 points for that and keep my NT script question still open for 250 points.
Can u tell me how to donate the 100 points to u. Can urecommend a book to teach me PERL?

This discussion is closed to new replies.