Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows command line to parse information from a text file

Tags:

batch-file

cmd

I have a text file which contains, amongst other information, lines that start with 'Password: ' . What I am looking to do is run a command line command (if possible) to parse through the .txt file to extract all of the terms that follow 'Password: ' and place them into a new textfile listing solely the passwords. The number of spaces between the word 'Password:' and the actual password varies in the entries.

I have so far used:

FINDSTR /L "Password: " input_file.txt > output_file.txt

This works well but extracts the term 'Password: ' too. From the research I have carried out, I don't believe Command Line has a built-in function to remove strings from .txt files does it as my thinking is to run the above command followed by something similar to:-

FINDSTR /L "Password: " input_file.txt > output_file.txt & DELSTR "Password: " output_file.txt > final_file.txt & DEL output_file.txt

Obviously I have invented the 'DELSTR' but as said, I would need something there to remove the string 'Password: ' from each line.

Any suggestions? Thanks

like image 301
thefragileomen Avatar asked Jul 15 '16 19:07

thefragileomen


1 Answers

in fact, there are some (very limited) built-in functions for string manipulation. See set /? for details.
No need to work with temporary files, you can parse your string directly into a variable:

(code edited to print all passwords, if there are more than one)

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /b /c:"Password: " input_file.txt') do (
  set pwd=%%a
  set "pwd=!pwd:Password: =!"
  echo !pwd!>>final_file.txt 
)
like image 53
Stephan Avatar answered Oct 24 '22 12:10

Stephan