2

I am writing a script in batch file for Windows. I want to replace double quotes with space in a file.

Input file File1.txt contains:

"05-09-2017", "07:00:14"
"05-09-2017", "07:00:14"
"05-09-2017", "07:00:14"
"05-09-2017", "07:00:14"
"05-09-2017", "07:00:14"

I have tried the following:

tr.exe "\"" " " < "File1.txt" >  "File2.txt"

The above line gives me an error like

tr.exe: too many arguments

And I have also tried:

sed.exe "s/\"/ /g" "File1.txt" >  "File2.txt"

The above gives me an error like

sed.exe: can't read >: Invalid argument

I need output file like this:

05-09-2017 , 07:00:14
05-09-2017 , 07:00:14
05-09-2017 , 07:00:14
05-09-2017 , 07:00:14
05-09-2017 , 07:00:14

Can you please look into this.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Jayesh
  • 39
  • 4

2 Answers2

3
sed "s/\x22/ /g" "input.txt" 

tr "\""" " "   < "input.txt"
tr """" " "    < "input.txt"
tr \""  " "    < "input.txt"
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Okay, I give up; I tried all sorts of parms for `tr` before seeing this answer; how/why does the first parm have the "extra" double quote: `"\"""`? – Mark Stewart Nov 06 '19 at 17:56
  • 1
    @MarkStewart, It is probably the worst way of writing it, but you need to include the additional quote (or escape a quote) to ensure the redirection operator is out of a quoted area so `cmd` handles it and it is not seen as a `tr` argument (same problem [here](https://stackoverflow.com/a/58661959/2861476)) – MC ND Nov 07 '19 at 19:49
0

Why using Unix tools and not native Windows commands for this simple task?

@echo off
if not exist "File1.txt" goto :EOF
setlocal EnableDelayedExpansion
del "File2.txt" 2>nul
for /F "usebackq delims=" %%I in ("File1.txt") do (
    set "Line=%%I"
    set "Line=!Line:"= !"
    echo !Line!>>"File2.txt"
)
endlocal

Note: The command FOR as used here skips empty lines and lines starting with a semicolon.

I would replace the double quotes by nothing which means deleting the space character after equal sign.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

Read also the Microsoft article about Using Command Redirection Operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Actually I am trying to grep some portion from a very big log file.Thats why i am using unix commands.Any way i will try this – Jayesh Jun 09 '17 at 08:58
  • To avoid possible interference with a trailing number I'd insert the stream number `echo !Line!1>>"File2.txt"` –  Jun 09 '17 at 09:04
  • @Jayesh you can avoid cmd quirks with PowerShell. It's much more powerful and predictable than cmd – phuclv Jun 09 '17 at 09:08