Search code examples
batch-filebatch-processing

How to parse command outptut and store in a file


I am executing command and parsing the command based on space, Formatting it and storing in a file.

This is how I have written the batch script

for /f "tokens=2,4,5" %G IN ('command') DO echo Id:%%G:~0,-1%  Timestamp:%H %I > C:\Users\TJ\Documents\out1.txt

I am getting the output like this

Id:3495068:~0,-1;  Timestamp:2023/01/25 14:57:18

But I am trying to trim the ";" semicolon but it's not trimmimg instead it's adding the trim logic to output.

I am expecting output like.

Id:3495068  Timestamp:2023/01/25 14:57:18

Is anything I am missing here.


Solution

  • Consecutive delimiters are treated as one. You split by Spaces, if you just add the semicolon to the delimiters, every space, every semicolon and every combination of those will be treated as (one!) delimiter.

    for /f "tokens=2,4,5 delims=; " %%G IN ('echo transaction 3495068; promote; 2023/01/25 14:57:16 ; user: thejas') DO echo Accurev Build ID:%%G  Timestamp:%%H 
    

    Output:

    Accurev Build ID:3495068  Timestamp:2023/01/25
    

    Note: the space must be the last char in delims=, or you get a syntax error.