Search code examples
batch-filefindstr

Batch print line until string found


I have a batch script that generates a file with the following structure (example)

123|etc|etc
345|etc|etc
678|etc|etc

I want my script to print out what it finds in each line, until it finds a |, so in this example i would want it to print:

123

345

678, and so on.

I've tried findstr /V with the | but it completely ignores the line (as the command states it will)

Any idea how could i turn this around?


Solution

  • @echo off
    for /f "usebackq tokens=1 delims=|" %%a in ("test.txt") do (
          echo %%a )
    
          pause
    

    This is looping through a file (test.txt) in the same directory as the batch script. I tell it to only use 1 token with a pipe delimiter then we just print out %%a which has the value of your first column of data.