Read file and show field number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sabercats
Posts: 6
Joined: 18 May 2011 15:54

Read file and show field number

#1 Post by sabercats » 29 Oct 2012 17:05

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

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: Read file and show field number

#2 Post by timbertuck » 29 Oct 2012 18:24

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

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Read file and show field number

#3 Post by Boombox » 29 Oct 2012 18:33

.

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...
Last edited by Boombox on 30 Oct 2012 05:57, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Read file and show field number

#4 Post by dbenham » 29 Oct 2012 22:13

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

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Read file and show field number

#5 Post by Boombox » 30 Oct 2012 06:09

.
Calm down Dave :)

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Read file and show field number

#6 Post by dbenham » 30 Oct 2012 07:04

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

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Read file and show field number

#7 Post by Boombox » 30 Oct 2012 07:15

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.

sabercats
Posts: 6
Joined: 18 May 2011 15:54

Re: Read file and show field number

#8 Post by sabercats » 30 Oct 2012 11:53

Thanks all, it works.

Post Reply