Removing carriage return and line feed from a text file

May 9, 2020 at 07:52:08
Specs: Windows 7
Hi guys,

looking for a bit of help if possible please. I have a text file which contains data like this:

123456789<CR><LF>
876543231<CR><LF>

I am trying to write a batch script which removes both <CR> and <LF>

I have found the below post which details how to remove the carriage return but cannot adapt it to also remove the line feed - post below

https://www.computing.net/answers/w...

any ideas on best how to approach this so that the end result would be a second file with contents like the below:

123456789
876543231

cheers

message edited by cableguy


See More: Removing carriage return and line feed from a text file

Reply ↓  Report •

#1
May 9, 2020 at 08:03:05
name it whatever.bat, dragg & drop a file onto it. it will create a file called output.txt without the <CR><LF>

@echo off&for /f "tokens=1,* delims=^<" %%a in (%1) do (echo %%a>>output.txt)

specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

#2
May 9, 2020 at 08:11:43
thanks for the response hidde663. How would i adapt the above so that it would read in a file name (which will always be static)
for example:

within batch script,
1) reads a file say mytext.txt (which lives in the same directory as the where the batch script will be saved)
2) removes the <CR> and <LF> with the code you have specified above
3) outputs a new file called mytext2.txt for example

cheers.


Reply ↓  Report •

#3
May 9, 2020 at 08:29:17
@echo off>mytext2.txt&for /f "tokens=1,* delims=^<" %%a in (mytext.txt) do (echo %%a>>mytext2.txt)

note that this will make mytext2.txt empty before reading mytext.txt

rename mytext2.txt after you run this.bat else the contents will be lost upon the second excecution

specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

Related Solutions

#4
May 9, 2020 at 08:31:53
this is maby a bit more readable:

@echo off
@echo off>mytext2.txt
for /f "tokens=1,* delims=^<" %%a in (
	mytext.txt
) do (
	echo %%a>>mytext2.txt
)

specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

#5
May 9, 2020 at 08:41:34
brilliant, just tried it, however still getting the same output result as the original file
so mytext2.txt contains:

123456789<CR><LF>
876543231<CR><LF>

rather then the expected

123456789
876543231


Reply ↓  Report •

#6
May 9, 2020 at 10:02:19
Try this in powershell:

Get-Content -Path "Folder\MyText.txt" | % { $_ -Replace "<CR><LF>", "" } | Out-File "Folder\MyText2.txt"

Edit: Tested this, and it worked.

message edited by Kilavila


Reply ↓  Report •

#7
May 9, 2020 at 11:30:57
weird, for me mytext2.txt contains:

123456789
876543231

the powershell from #6 should me more reliable btw ;) save as .ps1

specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

#8
May 9, 2020 at 12:47:57
You can give a try for this batch using vbscript to search and replace with regex :

@echo off
Mode 85,20 & color 0A
Title Replace String using Regex with vbscript
Set "InputFile=%~dp0content.txt"
Set "OutPutFile=%~dp0NewContent.txt"
echo(
:: To show Results on screen of console
Call :Search_Replace "%InputFile%" CON
:: To write Result in new file
Call :Search_Replace "%InputFile%" "%OutPutFile%"
echo(
echo Press any key to show the results in a new file :
echo "%OutPutFile%"
pause>nul
Start "" "%OutPutFile%" 
echo(
echo Did you want to update and replace all in your original file "%InputFile%" ?
Pause>nul
Move /Y "%OutPutFile%" "%InputFile%">nul
Start "" "%InputFile%" & Exit
::-----------------------------------------------------------------------------------
:Search_Replace <InputFile> <OutPutFile>
(
    echo WScript.StdOut.WriteLine Search_Replace(Data^)
    echo Function Search_Replace(Data^)
    echo Dim strPattern, strReplace, strResult,oRegExp
    echo Data = "%~1" 
    echo Data = WScript.StdIn.ReadAll
    echo strPattern = "(<CR><LF>)"
    echo strReplace = ""
    echo Set oRegExp = New RegExp
    echo oRegExp.Global = True 
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern
    echo strResult = oRegExp.Replace(Data,strReplace^)
    echo Search_Replace = strResult
    echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------


Reply ↓  Report •

#9
May 9, 2020 at 13:48:42
Been playing around with this a bit, heres a solution if you have multiple files in the same folder(and sub-folders) that you want to edit:

$Folder = Get-ChildItem -Path "Folder\Path" -Filter "*.txt*"
foreach ($File in $Folder){

Get-Content -Path $File | % { $_ -Replace "<CR><LF>", "" } | Out-File "Folder\Destination\Edited_$File"

}

Makes a copy of the files with "Edited_" infront of the original name.
Just have to change the folder paths then you can run it.
I just used -Filter "*.txt*" as an example to get all txt files from the folder, but you can use part of the filenames as the filter(assuming some part of each filename is the same).
If you want to get files from all sub-folders as well, then use "-Recurse" like so:

$Folder = Get-ChildItem -Path "Folder\Path" -Filter "*.txt*" -Recurse

Run this from powershell or save as .ps1

message edited by Kilavila


Reply ↓  Report •

#10
May 9, 2020 at 21:36:23
writes over the source-file, so MAKE SURE the source is BACKED UP. VBscript:
'begin vbscript 'nocrlf.vbs'
set fso=createobject("scripting.filesystemobject")
'repl can be null or most anything else. I used space...
repl=" "
'filename of target file is argument #1, as in: NOCRLF.VBS filename
z=wscript.arguments(0)
x=replace(fso.opentextfile(z).readall,vbcrlf,repl)
fso.opentextfile(z,2,true).write x

message edited by nbrane


Reply ↓  Report •

#11
May 10, 2020 at 00:11:49
Morning all,

Thanks for all the responses hidde663, Hackoo, Kilavila and nbrane.

So many options, I'll try all of the above and report back with results.

Appreciate all your efforts ;)


Reply ↓  Report •

#12
May 10, 2020 at 05:20:30
cableguy,
As ever, a solution starts with a statement of requirement.
Without CRLF, you don't have two lines; you have one.
Now, would you like to say what you're trying to do?

message edited by Mechanix2Go


Reply ↓  Report •

#13
May 12, 2020 at 01:41:24
Another CN hi-miler.
10 replies; nothing accomplished.
OP cut a chogi.

=====================

M2

message edited by Mechanix2Go


Reply ↓  Report •

#14
May 13, 2020 at 13:57:01
Hi guys,

Just reporting back with the results. I have tried three solutions (hackoo, kilavila and nbrane). All three have worked and given me what I required.

Thanks for all the help guys.


Reply ↓  Report •

Ask Question