?
Solved

batch remove <CR><LF> from last line only in text file

Posted on 2013-01-28
7
Medium Priority
?
3,437 Views
Last Modified: 2013-01-29
I have script which works fine as I expected it  remove <CR><LF> from last line only in text file. But if there is space or tab in record it skips everything. So I want to remove only last line CR CF. Given scripts scans all lines from target text file, removes blank lines (I don't want this. Only scan last line and remove CR CF) and append into destination file.

bcp TRDS.dbo.tbltestExtracts out "C:\Data\CAAGIndividualsFile.TXT" -c -testinstance\T_GSS -Utest -Ptest
call :StripBlankLines CAAGIndividualsFile.TXT

rem del CAAGIndividualsFile.TXT 
rem ren temp_CAAGIndividualsFile.TXT CAAGIndividualsFile.TXT 

goto :eof
:StripBlankLines
For %%x in ("%~1") do set OutF=temp_(%%~nx).txt
if exist "%OutF%" del "%OutF%"
set FirstLine=1
for /F "usebackq delims=" %%B in (%*) do (
	call :TrimWS %%B
	if not "!Line!"=="" (
		if "!FirstLine!"=="1" (
			set FirstLine=0
		) else (
			>>"%OutF%" echo.
		)
		call :write !Line!
	)
)
goto :eof

:TrimWS
set Line=%*
goto :eof 
 
:write
>>"%OutF%"<NUL set /p Dummy=%*
goto :eof           

Open in new window

0
Comment
Question by:sonawanekiran
7 Comments
 
LVL 16

Expert Comment

by:Chris H
ID: 38829838
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 38830244
The script below will keep (nearly; the only thing it will remove is leading white spaces in the very last line) all lines as they were, and strip away the last <CR><LF>.
Note that the script you posted won't work as expected; you left out the "setlocal enabledelayedexpansion" line, which is vitally important for the function.
@echo off
setlocal enabledelayedexpansion
bcp TRDS.dbo.tbltestExtracts out "C:\Data\CAAGIndividualsFile.TXT" -c -testinstance\T_GSS -Utest -Ptest
call :StripLastCRLF "C:\Data\CAAGIndividualsFile.TXT"

rem del CAAGIndividualsFile.TXT 
rem ren temp_CAAGIndividualsFile.TXT CAAGIndividualsFile.TXT 

goto :eof
:StripLastCRLF
for %%a in ("%~1") do set OutF=%%~dpa\temp_(%%~na).txt
for /f %%a in ('type "%~1" ^| find /v /c ""') do (set TotalLines=%%a)
if exist "%OutF%" del "%OutF%"
set /a LineCount=0
for /f "tokens=1* delims=[]" %%a in ('type "%~1" ^| find /v /n ""') do (
	set /a LineCount += 1
	if !LineCount!==!TotalLines! (
		>>"%OutF%"<NUL (set /p Dummy=%%b)
	) else (
		>>"%OutF%" echo.%%b
	)
)
goto :eof

Open in new window

0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38830460
I already tried this. Please try to run the script against attached test file. (comment the bcp line). You will come to know.. Please...
test.txt
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 17

Author Closing Comment

by:sonawanekiran
ID: 38830509
@oBdA: Thank you Sir. It seems working... Thank you again  500*4
0
 
LVL 85

Expert Comment

by:oBdA
ID: 38830513
The issue with that file is that there's a NUL (ASCII 0) in the first line of that file (check it in a hex editor or in something like Notepad++ with "Show all characters" enabled).
This character shouldn't appear in a text file at all, so you need to check your database how it slipped into a text field in the first place and then made its way into an export.
You can expect all kinds of strange behavior when trying to modify a "text" file that contains non-printable characters other than TAB/CR/LF.
The first line looks like actually like this:
Omega Associates, L.L.C[tab]13-3973682[tab][tab]88 Pine Street 31st Floor[tab][NUL][tab][tab][tab][tab][tab][tab][tab][tab][CR][LF]

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38830710
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

809 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