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
Solved

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

Posted on 2013-01-28
7
2,922 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
ID: 38829838
0
 
LVL 84

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 17

Author Closing Comment

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

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 17

Author Comment

by:sonawanekiran
ID: 38830628
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38830710
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
robocopy script with data and file filter restrictions 3 26
Picture size 4 35
Selecting Right Partition 6 68
Create .bat File 16 28
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

840 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