Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Replace white space with tab in text file

Posted on 2011-05-02
6
Medium Priority
?
2,033 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:bravotango
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 35510549
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
 

Author Comment

by:bravotango
ID: 35512030
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
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 35512113
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
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 

Author Comment

by:bravotango
ID: 35512449
Thanks that worked. Could you tell what the s/ and the /g mean?
0
 

Author Closing Comment

by:bravotango
ID: 35512478
Appreciate your help and the extra tutoring on sourceforge.net
0
 
LVL 85

Expert Comment

by:oBdA
ID: 35512555
"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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

810 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