Solved

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

Posted on 2013-01-28
7
2,759 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:choward16980
Comment Utility
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Author Closing Comment

by:sonawanekiran
Comment Utility
@oBdA: Thank you Sir. It seems working... Thank you again  500*4
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
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 17

Author Comment

by:sonawanekiran
Comment Utility
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
0

Featured Post

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Join & Write a Comment

Suggested Solutions

Several part series to implement Internet Explorer 11 Enterprise Mode
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now