0

I've got a text file with following line:

17,60,"10 Ursae Majoris",0.03125,34.90625,-39.09375,0,0,176,None,5,None,64,Anarchy,10,None,,,,0,1678643440,1678643440,"10 Ursae Majoris",,,3,Common,2415659059555

and need to get the name inside the first quotation marks, so 10 Ursae Majoris the second quotation marks should be ignored.

my code can't work, as I'm on Windows Batch and not on linux:

echo off

for /f "delims=," %%i in (systems.txt) do echo %%i | grep -o '([^""]*)' | head -1

what is the Windows Batch equivalent? Many thanks.

searched and found Regex - Match only first instance of brackets

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Is this what you mean? ```@For /F UseBackQ^ Tokens^=2^ Delims^=^" %%G In ("systems.txt") Do @Echo %%G``` – Compo Mar 14 '23 at 23:16
  • use powershell instead which has builtin capability to deal with json, csv, xml... directly: `(cat file | ConvertFrom-Csv -Header 'a','b','c').c`. For example `(echo '17,60,"10 Ursae Majoris",0.03125,34.90625,-39.09375,0,0,176,None,5,None,64,Anarchy,10,None,,,,0,1678643440,1678643440,"10 Ursae Majoris",,,3,Common,2415659059555' | ConvertFrom-Csv -Header 'a', 'b', 'c').c` – phuclv Mar 15 '23 at 01:18
  • `for /f "tokens-3delims=," %%i in (systems.txt) do echo %%~i` Assuming that the required string is always the third comma-separated token – Magoo Mar 15 '23 at 05:51
  • That should be `tokens=3` of course... – Magoo Mar 15 '23 at 09:44
  • This works: `for /F "delims=" %%i in (systems.txt) do set "line=%%i"` line `set "line=%line:*"=%"` line `set "name=%line:"=" & rem "%"` – Aacini Mar 15 '23 at 16:55
  • I appreciate your help, but I get a "cannot be processed syntactically at this point" with every one of your for-loops. Do you have an idea? Many thanks. – Franklyn W. R. Tigier Mar 15 '23 at 18:25

1 Answers1

0

This is my systems.txt test file:

17,60,"10 Ursae Majoris",0.03125,34.90625,-39.09375,0,0,176,None,5,None,64,Anarchy,10,None,,,,0,1678643440,1678643440,"10 Ursae Majoris",,,3,Common,2415659059555

This is my Batch code:

@echo off
setlocal
for /F "delims=" %%a in (systems.txt) do set "line=%%a"
set "line=%line:*"=%"
set "name=%line:"=" & rem "%"
echo %name%

This is the output:

10 Ursae Majoris
Aacini
  • 65,180
  • 12
  • 72
  • 108