Use quotas as delim skipping two lines in batch script

60 Views Asked by At

I'm stuck on a statement that I've already used multiple times with other delimiters. I have to process a three-line TXT file, each line contains 26 fields delimited by quotes. This is the file in which the second set is empty, it's a possible case:

ID="a1i7Q000000gTynQAE" Machine="TEST" Dept="TEST" ExecutionDate="2023/11/17" 
Set 1="O14" Time="2023/11/17 18:33:09" MeasurementMode="Rad" A1="40.96" A2="41.65" A1Lenght="177" n1="133.75" C1="527" CAD="2.48" LAT="4.43" L="23.88" RoN="12.52" Diam="5.23"/ 
Set S="152" Time="2023/11/17 18:33:09" MeasurementMode="Deg" A1="NaN" A2="NaN" A1Lenght="NaN" n1="132.14" C1="NaN" CAD="NaN" LAT="NaN" L="NaN" RoN="NaN" Diam="NaN"/ 

On the first pass I have to extract data %%A, %%H, %%J, %%L, %%N, %%P and so on ignoring the first row, on the second pass I have to ignore the first two rows.

The expected output for result1.txt is

a1i7Q000000gTynQAE
"VV_A1_c" : 40.96
"VV_j32_a" : 41.65
177
.....

The command lines I'm using are the following:

for /f tokens^=1-26^ skip^=1^ delims^="^ %%A in ("out1.txt") do (
 ECHO %%~A >>result1.txt
 ECHO "VV_A1_c" : %%~H >>result1.txt
 ECHO "VV_j32_a" : %%~J >>result1.txt
 ECHO %%~L >>result1.txt
 .....

and

for /f tokens^=1-26^ skip^=2^ delims^="^ %%A in ("out1.txt") do (
 ECHO %%~A >>result2.txt
 ECHO "VV_A1_c" : %%~H >>result2.txt
 .....

...but the result is always "Sintax error" or "not expected" no matter how I set the ^ delimiters. Please can anyone tell me where I'm wrong?

Another question: if the fields become 30 what letters must I use after %%Z?

1

There are 1 best solutions below

5
Luuk On

Tried to get it working using Windows batch:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION
set A=0
echo A:%a%::
for /f delims^=^"^ tokens^=1-26 %%B in (out1.txt) do (
    SET /A A=!A!+1
    IF !A! GEQ 2 (
         echo Line!A!:FieldB:%%B
         echo Line!A!:fieldC:%%C
         echo Line!A!:FieldD:%%D
         echo Line!A!:FieldZ:%%Z
         echo.
    )
)

output:

A:0::
Line2:FieldB:Set 1=
Line2:fieldC:O14
Line2:FieldD: Time=
Line2:FieldZ: Diam=

Line3:FieldB:Set S=
Line3:fieldC:152
Line3:FieldD: Time=
Line3:FieldZ: Diam=

But skip= need too much work-around and i am no-where near your expected output.

Maybe a simpler approach would be PowerShell:

Import-CSV out1.txt -Delimiter '"' -Header ('a','b','c','d') | Format-Table

output:

a      b                  c        d
-      -                  -        -
ID=    a1i7Q000000gTynQAE Machine= TEST
Set 1= O14                Time=    2023/11/17 18:33:09
Set S= 152                Time=    2023/11/17 18:33:09

For your expected output this might start with:

Import-CSV out1.txt -Delimiter '"' -Header ('a','b','c','d','e','f','g','VV_A1_c','i','VV_j32_a','k','l','m','n','o','p','q','r') | Format-Table VV_A1_c,VV_j32_a,l,n,p

output:

VV_A1_c    VV_j32_a l   n      p
-------    -------- -   -      -
2023/11/17
40.96      41.65    177 133.75 527
NaN        NaN      NaN 132.14 NaN

And, a (steep?) learning curve towards PowerShell, which I am not too familiar with...