Extracting data from ever changing .txt

December 15, 2009 at 11:12:34
Specs: Windows XP
I have the following issue with a txt file that changes on a daily basis.

My text file contains the following data:
Changeset User
--------- -------------
3990 xxxxx

Looking to create a batch file that will extract "3990" and place it into another .txt file. The only issue is that "3990" will never be the same value. However it will always be the first 4 characters of the 3rd line.

Do you have any advice?


See More: Extracting data from ever changing .txt

Report •


#1
December 15, 2009 at 11:51:03
Here you go:

C:\>type test.txt
Changeset User
--------- -------------
3990 xxxxx

More lines


And more

C:\>for /f "skip=2 tokens=1 delims= " %a in (test.txt) do echo myvar=%a | findstr "[0-9][0-9][0-9][0-9] "

C:\>echo myvar=3990 | findstr "[0-9][0-9][0-9][0-9] "
myvar=3990

C:\>echo myvar=More | findstr "[0-9][0-9][0-9][0-9] "

C:\>echo myvar=And | findstr "[0-9][0-9][0-9][0-9] "

C:\>


You can change the command after the "do" word. I've made it so that it sets a variable, but you can redirect to another file instead as well:

C:\>for /f "skip=2 tokens=1 delims= " %a in (test.txt) do echo %a| findstr "[0-9][0-9][0-9][0-9] " >> result.txt

C:\>echo 3990 | findstr "[0-9][0-9][0-9][0-9] " 1>>result.txt

C:\>echo More | findstr "[0-9][0-9][0-9][0-9] " 1>>result.txt

C:\>echo And | findstr "[0-9][0-9][0-9][0-9] " 1>>result.txt

C:\>type result.txt
3990

C:\>


You must use ">>" instead of ">"


Report •
Related Solutions


Ask Question