3

To delete tab at the end of each line of a text file, I do

findstr /r /v /c:"[ ]$" "~1">result.txt

But tab is still there.

a file example

  John Doe
  Bob Materson
  ..........

After John Doe, Bob Materson, ..., it may have some tab. I want to eliminate them so that my output file ("result.txt") has no longer them.

Having a search on line, I see that it is possible to use the 'sed' command to perform it but I wouldn't like to install that on PC. So if there is a solution which does not use it, I really wish to thank you in anticipation for.

Endoro
  • 34,892
  • 8
  • 45
  • 61
new
  • 939
  • 5
  • 20
  • 29

5 Answers5

4

when I had a similar problem I used the more command, which is similar to the type command, except with parameters. Here is the command:

more /T0 [File name] > results.txt

Where the /T0 specifies to convert each tab into 0 spaces, essentially getting rid of them. Note this will get rid of every tab in the .txt file.

Hope this helped.

Yours Mona.

Monacraft
  • 6,187
  • 1
  • 13
  • 27
  • i have tried either more +5 /T0 "~1"> results.txt or more /T0 +5 "~1"> results.txt; But Tab is still present at the end of lines. I have to use more +5 to skip some lines). Thanks – new Jun 25 '13 at 06:57
  • 1
    Ok. I've tried this code myself and it has worked perfectly, so any problems and you will need to look for something else. `type [file name] | more /T0 +5 > results.txt`. Yours Mona. – Monacraft Jun 25 '13 at 07:09
  • 1
    It's a neat solution - but just be aware that at each 64K lines MORE pauses for user input. If your file is less than that in lines then no problemo. – foxidrive Jun 25 '13 at 08:46
  • It's very strange! Though each line is not greather that 64K, I don't understand why type myfilename | more /T0 +5 > results.txt does work at all while it has worked at you!!! The file 'results.txt' still contains tabs at the end of each line – new Jun 25 '13 at 09:03
  • This seems interesting. Just to clarify, I'm using windows 7 with Ms-Dos of version 6.1.7601. Not that it should make a great difference. Also I recommend you try using vb script for this since it can offer many alternate solutions when your trying to do something in batch. – Monacraft Jun 25 '13 at 10:42
  • It didn't work for me in Git Bash, but it worked perfectly on the regular Command Prompt (Windows). – JCarlosR Oct 23 '19 at 18:34
2
@echo off
rem Insert an EndOfFile mark in original file (choose one good for you):
echo :EOF:>> file.txt
call :CopyLines < file.txt > result.txt
goto :EOF

:CopyLines
   set line=
   set /P line=
   if "%line%" equ ":EOF:" exit /B
   echo(%line%
goto CopyLines
Aacini
  • 59,374
  • 12
  • 63
  • 94
1

check for one tab at the line's end and remove if it present:

@echo OFF &SETLOCAL
FOR /f "delims=" %%i  IN ('findstr /n "^" file') DO (
    SET "line=%%i"
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET "line=!line:*:=!"
    ECHO(%%i|FINDSTR /r /c:".*  $" >NUL && ECHO(!line:~0,-1! || ECHO(!line!
    REM      this is a <tab>--^
    ENDLOCAL
)

You should better use GNU sed for that:

sed s/\t$// file
Endoro
  • 34,892
  • 8
  • 45
  • 61
0

This is not a general solution, but if you're sure you do not have any tabs in the text itself (only trailing and possibly leading) you could use for /f directly from command line:

for /f "delims=       " %L in (file.txt) do @echo %L >result.txt

This will strip any leading/trailing tabs from data - but if you have any tabs in text, it will also result in incomplete lines!

Character after delims= needs to be actual Tab (do not copy/paste from here, either prepare in editor or start cmd with /f:off switch to avoid tab expansion).

wmz
  • 3,525
  • 1
  • 12
  • 21
0

This is nothing more than a special case of text file editing. There are lots of options provided at How can you find and replace text in a file using the Windows command-line environment?

My favorite option is a hybrid JScript/Batch utility called REPL.BAT that provides regex search and replace functionality to batch programming and the command line. The script does not require installation of any 3rd party executable, and it is compatible with any modern Windows platform from XP onward.

Assuming you have REPL.BAT either in your PATH or else in your current directory, then all you need is:

type "%~1"|repl "\t*$" "" >"%~1.new"
move /y "%~1.new" "%~nx1" >nul
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353