Replace white space with tab in text file

Hello,

I have a new tide chart every month and it is full of unwanted spaces which gives the appearance of tabs.

I would like to substitute all the spaces except where there are two or less with a TAB.

I have already got the regex for this substitution / {3,}/, \t

I also found this bat code but it is difficult to use and hard to modify.

REM This batch allows string substitution in a text file. It parses each line of a text file
REM for a particular string and replaces it with another string.
REM To replace all occurrences of "Yellow Submarine" in "color.txt" with "uboot"
REM and put the output on the screen run:
REM BatchSubstitute.bat "Yellow Submarine" uboot color.txt

SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)

I also found something similar here
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_24501506.html?sfQueryTermInfo=1+10+30+substitut

but can't understand how I could use that for my problem.

I don't want to enter the file name and the search and replace string. Just want to use the regex search and replace and generate a new file. The file name is Tides.txt

bravotangoAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You only need sed.exe, and it can either be somewhere in the path or just in the script folder.
Sorry, something went wrong during copy and paste above. The version below should make more sense:
@echo off
setlocal
set InFile=Tides.txt
set OutFile=Tides-tabbed.txt
REM *** Replace [TAB] in the following line with an actual tab character:
sed.exe "s/ \{3,\}/[TAB]/g" "%InFile%" >"%OutFile%"

Open in new window

0
 
oBdACommented:
You can use sed.exe for this (included in the UnxUtils.zip archive here: http://unxutils.sourceforge.net/).
Unfortunately, most sed versions don't know "\t", so you'll have to enter a tab character in the batch script below.
@echo off
setlocal
set InFile=Tides.txt
set OutFile=Tides-tabbed.txt
REM *** Replace [TAB] in the following line with an actual tab character:
sed.exe s/ \{3,\}/[TAB]/g >"%OutFile%"

Open in new window

0
 
bravotangoAuthor Commented:
Sorry tried that and it didn't work. I downloaded all those files from sourceforge.net but there is no guide on where to put them. I am not keen to just copy them into Windows/System32 folder.

I just copied one file, namely sed.exe into my scripts folder to try it and not successful.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
bravotangoAuthor Commented:
Thanks that worked. Could you tell what the s/ and the /g mean?
0
 
bravotangoAuthor Commented:
Appreciate your help and the extra tutoring on sourceforge.net
0
 
oBdACommented:
"s/<searchfor>/<replacewith>/<options>" tells sed to replace <searchfor> with <replacewith>, and "g" in the options means to globally replace (otherwise only the first match in a line will be handled).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.