How to extract a string from last line in a text file -windows batch script
10,056
If there are always 4 lines as shown with "TRAILER" in the last line:
for /f "skip=3 tokens=2 delims=|" %l in (List.txt) do @echo %l
If there can be any number of lines with the "TRAILER" line being present anywhere:
for /f "tokens=2 delims=|" %l in ('type List.txt ^| find "TRAILER"') do @echo %l
for /?
will teach you what the skip, tokens and delims options mean, although it shouldn't be hard to figure out. Also remember to double each % sign while using the commands in a batch file.
Related videos on Youtube
Author by
Novice
Updated on September 18, 2022Comments
-
Novice over 1 year
I have a .txt file in this format
123|abc|23456 234|rsty|68589 890|gfyu|5679 TRAILER|3|
How do i extract "3" from the last line enclosed between "TRAILER|" and "3" ? Need help.
-
DavidPostill almost 9 yearsIs this a one off task? If not, are there always 4 lines in the file?
-
-
DavidPostill almost 9 yearsHehe. I was waiting for a reply to my comment before answering, so you beat me to it ;)
-
Karan almost 9 years@DavidPostill: I knew you would have an answer ready as well. :) Anyway I tried to cover the scenarios I could think of but who knows, the OP might get back with a completely different set of requirements.