How do I read lines from a text file in the order presented using windows batch script?

I have a text file that looks like this

number 1
some junk goes here
fruit: apple
number 2
some stuff goes here
fruit: banana
number 3
some stuff goes here
fruit: orange

My script looks like this

@echo off

setlocal EnableDelayedExpansion

set input_file=testfile.txt

for /f "tokens=2" %%a in (
    'findstr " 1 2 3" %input_file%'
) do echo      %%a

for /f "tokens=*" %%b in (
    'findstr "fruit" %input_file%'
) do echo      %%b

My current code is giving me this

1
2
3
fruit: apple
fruit: banana
fruit: orange

How do I get the output formatted like this?

1
fruit: apple
2
fruit: banana
3
fruit: orange

Thanks in advance.



Read more here: https://stackoverflow.com/questions/68476556/how-do-i-read-lines-from-a-text-file-in-the-order-presented-using-windows-batch

Content Attribution

This content was originally published by jdmeyer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: