Solved

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

Posted on 2013-01-28
7
3,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Expert Comment

by:choward16980
ID: 38829838
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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 55

Expert Comment

by:Bill Prew
ID: 38830710
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

717 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