Page 1 of 1

parse text file

Posted: 06 Jan 2015 15:23
by joecepy
Hello,

I'm hoping someone can assist with the following. I have a psftp text file contains a lot of gibberish.

Code: Select all

Remote directory is now /xjp/utility/CapEx/Inbound/Program
psftp> ls *.csv
Listing directory /xjp/utility/CapEx/Inbound/Program
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 program2.csv
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 program5.csv
psftp> cd /xjp/utility/CapEx/Inbound/Program/ProgFinancial/
Remote directory is now /xjp/utility/CapEx/Inbound/Program/ProgFinancial
psftp> ls *.pdf
Listing directory /xjp/utility/CapEx/Inbound/Program/ProgFinancial
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_program2.pdf
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_program5.pdf
psftp> cd /xjp/utility/CapEx/Inbound/Project/
Remote directory is now /xjp/utility/CapEx/Inbound/Project
psftp> ls *.csv
Listing directory /xjp/utility/CapEx/Inbound/Project
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 project1.csv
psftp> cd /xjp/utility/CapEx/Inbound/Project/ProjFinancial/
Remote directory is now /xjp/utility/CapEx/Inbound/Project/ProjFinancial
psftp> ls *.pdf
Listing directory /xjp/utility/CapEx/Inbound/Project/ProjFinancial
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_project1.pdf
psftp> quit


I want to search for following line of string in a text file that contains "-rw-r--r--" and parse out the filename so it can vary from project1.csv or Financial_Analysis_project1.pdf, etc.

for example I'd read in the following line of string

Code: Select all

-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 project1.csv


and parse out

Code: Select all

project1.csv


Is this doable or am I reaching?

thanks

Re: parse text file

Posted: 06 Jan 2015 16:06
by joecepy
found solution. I was racking my brain but it was simpler that I expected.

Re: parse text file

Posted: 06 Jan 2015 18:10
by Squashman
joecepy wrote:found solution. I was racking my brain but it was simpler that I expected.

And your solution was?

Re: parse text file

Posted: 06 Jan 2015 20:24
by joecepy
Squashman wrote:
joecepy wrote:found solution. I was racking my brain but it was simpler that I expected.

And your solution was?


Code: Select all

@ECHO OFF &SETLOCAL
for /f "delims=" %%a in ('^<20150601_150617.scp.log find "-rw-r--r--"') do call:DOit "%%~a"
goto:Eof

:doit
setlocal
set "string=%~1"
set "STring=%string:*-rw-r--r--=%"
for /f "Tokens=8" %%b in ("%string%") do echo(%%b >> transferFile.log
exit /b

Re: parse text file

Posted: 06 Jan 2015 20:49
by Squashman
You can do this and it will be much much much faster. Using CALL slows down your batch file by a lot. Granted you only have a handful of files but if you were trying to parse a couple hundred lines you will notice a difference in speed.

Code: Select all

@ECHO OFF
for /f "tokens=9 delims= " %%a in ('find "-rw-r--r--" 20150601_150617.scp.log') do >>transferFile.log echo %%~a

Re: parse text file

Posted: 06 Jan 2015 20:52
by Squashman
Slight edit. Just in case your file names actually have spaces.

Code: Select all

@ECHO OFF
for /f "tokens=8* delims= " %%a in ('find "-rw-r--r--" 20150601_150617.scp.log') do >>transferFile.log echo %%~b