Page 1 of 1

Read file and show field number

Posted: 29 Oct 2012 17:05
by sabercats
I have a flat files ; ex A.txt

Code: Select all

10/29/2012 1:37 PM|Available|sabercats-pc|San Jose|sanjose@somewhere.com|Win7|NCFF|Release|sanjoseWin7|AAA123|1351546670
10/29/2012 1:38 PM|Available|tigers-pc|New York|newyork@somewhere.com|Win8|NCFF|Releasing|tigersWin8|AAA123|1351546736


How do i get field sanjoseWin7 and tigersWin8 ?

and what is field sabercats-pc and tigers-pc

I wrote it like this but did not work, cannot give me
sanjosewin7 sabercats-pc
tigers-Win8 tigers-pc

Code: Select all

for /f "tokens=1,2,3,4,5 delims=|" %%a in (A.txt) do (
echo %%j %%c
)

Thanks

Re: Read file and show field number

Posted: 29 Oct 2012 18:24
by timbertuck
you were close so try this

Code: Select all

@echo off
for /f "tokens=1-11 delims=|" %%a in (A.txt) do (
   rem first token is %%a
   rem third token is %%c
   rem ninth token is %%i
   echo %%i
   rem last token is %%k
   echo %%i %%c
)


that should fit both of your scenarios

Re: Read file and show field number

Posted: 29 Oct 2012 18:33
by Boombox
.

Code: Select all

@echo off
for /f "tokens=3,9 delims=|" %%g in (file.txt) do echo %%g %%h
pause>nul


sabercats-pc and tigers-pc are in the 3rd token if the delims are set to | & sanjosewin7, the 9th.


NOTE: If you are going to want 5 tokens, you will need to give 5 variable names, i.e. do echo %%a %%b %%c %%d %%e

Using | as the delims. splits the line up into 11 sections... So each block of data before each | is treated as a single entity.

Date & Time (1st/%%a) | Available (2nd/%%b) | PC-Name(3rd/%%c) | etc...

Re: Read file and show field number

Posted: 29 Oct 2012 22:13
by dbenham
If you want the 3rd and 9th tokens, then ask for them.

Code: Select all

for /f "tokens=3,9 delims=|" %%a in (A.txt) do echo %%b %%a


Dave Benham

Re: Read file and show field number

Posted: 30 Oct 2012 06:09
by Boombox
.
Calm down Dave :)

I'm sure he's just not fully understood the FOR structure yet....

Re: Read file and show field number

Posted: 30 Oct 2012 07:04
by dbenham
Boombox wrote:.
Calm down Dave :)

I'm sure he's just not fully understood the FOR structure yet....

There is no agitation in my answer, and it was not intended to be rude. I just tersely stated the solution.

My answer does look redundant now that you have edited your answer. I'm pretty sure you did not have the virtually identical code prior to the edit, although the order of the output in your code is reversed from the OP's request.


Dave Benham

Re: Read file and show field number

Posted: 30 Oct 2012 07:15
by Boombox
If you want the 3rd and 9th tokens, then ask for them.


There is no agitation in my answer


My answer does look redundant now that you have edited your answer. I'm pretty sure you did not have the virtually identical code prior to the edit,


I put spaces on the last line of my post to make the example easier to read... Nothing else Dave.

Re: Read file and show field number

Posted: 30 Oct 2012 11:53
by sabercats
Thanks all, it works.