1

I would like to search & replace the end of each line in my file.txt and delete the char before.

In other words, I have a redundant comma at end of each line, and it disturbs me when passing the data to Excel.

I can't delete all commas, since most of them are valuable. I just need to delete those at end of each line, since they make a blank cell.

I don't mind using a PowerShell gc command.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
giladpac
  • 19
  • 3

3 Answers3

1

This is the simplest way to "delete the last char of each line" using a Batch file:

@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in (file.txt) do (
   set "line=%%a"
   echo(!line:~0,-1!
)) > output.txt

This Batch file have a series of limitations: it removes empty lines and lines that start with semicolon, and remove exclamation marks from the lines. Each restriction can be fixed introducing a modification that eventually ends in a code similar to aschipfl's answer, but this code may be enough for your needs.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I was thinking about posting such code as well, but I decided not to do it that simple as the OP said "search & replace"... +1 anyway as it's compact and well described... – aschipfl Dec 06 '15 at 18:21
0

Here is a PowerShell script that will take file in.txt as input, remove the last character from each line, and output them to out.txt:

Get-Content "in.txt" | % { $_.Substring(0, ($_.Length - 1)) } | Out-File "out.txt"

Adding a simple test to check you are only deleting commas:

Get-Content "in.txt" | % {
    if($_.Substring(($_.Length - 1), 1) -eq ",") {
        $_.Substring(0, ($_.Length - 1))
    } else {
        $_
    }
} | Out-File "out.txt"
sodawillow
  • 12,497
  • 4
  • 34
  • 44
0

Here is a pure solution:

The following code snippet removes the last character from each line in file.txt if it is , and writes the result into file_new.txt:

@echo off
setlocal EnableExtensions
> "file_new.txt" (
    for /F usebackq^ delims^=^ eol^= %%L in ("file.txt") do (
        setlocal DisableDelayedExpansion
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        if "!LINE:~-1!"=="," (
            echo(!LINE:~,-1!
        ) else (
            echo(!LINE!
        )
        endlocal
        endlocal
    )
)
endlocal

The toggling of delayed environment variable expansion (see setlocal /? and set /? for help) is required to avoid trouble with some special characters.

The above approach removes empty lines from the file as for /F ignores them. To avoid this, use the following script:

@echo off
setlocal EnableExtensions
> "file_new.txt" (
    for /F delims^=^ eol^= %%L in ('findstr /N /R "^" "file.txt"') do (
        setlocal DisableDelayedExpansion
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:*:=!"
        if "!LINE:~-1!"=="," (
            echo(!LINE:~,-1!
        ) else (
            echo(!LINE!
        )
        endlocal
        endlocal
    )
)
endlocal

This uses the fact that findstr /N /R "^" returns every line (even empty ones) of the given file and prefixes it with a line number plus :. Therefore no line appears to be empty to for /F. The line set "LINE=!LINE:*:=!" is inserted to remove that prefix (everything up to the first :) from each line prior to outputting.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • What do you mean by "does not work"? please regard that the working directory must be the one where `file.txt` is located; then it should work... – aschipfl Dec 06 '15 at 18:15
  • See my [edit](http://stackoverflow.com/revisions/34118273/3): I corrected a typo in the `if "!LINE:~-1!"==","` line (the 2nd `!` was missing); then I improved the `echo ` syntax (`echo(`); and I added the `@echo off` command at the beginning... – aschipfl Dec 14 '15 at 03:16