how to select some row values of one column based upon a txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

how to select some row values of one column based upon a txt file

#1 Post by goodywp » 07 Nov 2017 14:18

I have a task to select certain row values of one column from a txt file

1) I have a criteria.txt
T501-08667-0102
T501-08696-0101
T501-08657-0101

2) I have another txt file (profile_scheme.txt) which has four column as below:

T501-08667-0100, T501-08680-0100, T501-08815-0100, T501-08665-0100
T501-08667-0101, T501-08680-0101, T501-08815-0101, T501-08665-0101
T501-08667-0102, T501-08680-0102, T501-08815-0102, T501-08665-0102

T501-08696-0100, T501-08699-0100, T501-08700-0100, T501-08666-0101
T501-08696-0101, T501-08699-0101, T501-08700-0101, T501-08666-0101

T501-08657-0100, T501-08690-0100, T501-08820-0100, T501-08664-0100
T501-08657-0101, T501-08690-0101, T501-08820-0101, T501-08664-0101

I have a code to get whole column, say column 3

Code: Select all

for /f "tokens=3 delims=, " %%a in (profile_scheme.txt) do (
    echo%%a>>sch_pack_need.txt
)
So I got the following sch_pack_need.txt

T501-08815-0100
T501-08815-0101
T501-08815-0102
T501-08700-0100
T501-08700-0101
T501-08820-0100
T501-08820-0101

But what I want is to get 3 row values (in bold) from this column which is referencing from criteria.txt
T501-08815-0102
T501-08700-0101
T501-08820-0101

Thanks
Last edited by goodywp on 09 Nov 2017 10:45, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: how to select some row values of one column based up a txt file

#2 Post by Squashman » 07 Nov 2017 15:39

FINDSTR and the /G option are your friend.

Criteria.txt does not match your bolded items so I am not sure how you want it matched.

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to select some row values of one column based up a txt file

#3 Post by goodywp » 07 Nov 2017 16:11

What I want to match is the last four digits which is the version in criteria.txt

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: how to select some row values of one column based up a txt file

#4 Post by Squashman » 07 Nov 2017 17:00

goodywp wrote:What I want to match is the last four digits which is the version in criteria.txt
Then why are you excluding T501-08815-0101

I think what you are trying to do is match criteria.txt to the first column in profile_scheme.txt and then output the third column from profile_scheme.txt to sch_pack_need.txt.

So here you go. Option one matches on the last 4 digits of Criteria.txt to the last 4 digits of column 3 in profile scheme. Option 2, matches criteria.txt to column 1 of profile_scheme.txt.

Code: Select all

@echo off

REM OPTION 1
(for /f "tokens=3 delims=-" %%G in (criteria.txt) do echo ....-.....-%%G)>search.txt

(for /f "tokens=3 delims=, " %%H in (profile_scheme.txt) do (
    echo %%H|findstr /G:search.txt
))>sch_pack_need.txt
del search.txt


REM OPTION 2
(for /F "tokens=3 delims=, " %%I IN ('findstr /B /G:criteria.txt profile_scheme.txt') do echo %%I)>sch_pack_need2.txt

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: how to select some row values of one column based up a txt file

#5 Post by goodywp » 08 Nov 2017 08:59

Thanks Squashman!!!

The option 2 is the exactly one I need. I was thinking to use the last four digits as a search criteria to do the job. But it came out the one you mentioned which should be excluded...

Instead, it is better to use the whole strings in column 1 to get the corresponding rows in column 3.

Thanks again!!!

W

Post Reply