Solved

Search for a string in file and get the next value in it

Posted on 2011-02-22
9
318 Views
Last Modified: 2012-05-11
Example text file contains the below values. I need a batch file to get the value 'sm10' when given 203 as input.
The below master file is one time prepared and values can be seperated other than comma if required.

203,sm10
304,sm20
0
Comment
Question by:sk7815
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34956178
Is that really for MS-DOS like specified in your question zone, or a command prompt of some Windows version?



0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34956226
Assuming your data is in file "foo.dat" and this command line is in batch file "foo.bat"

@FOR /F "tokens=1,2 delims=," %%i IN (foo.dat) DO @IF "%%i" == "203" echo %%j

Open in new window


c:\temp>foo.bat
sm10

Open in new window


You didn't say what you wanted to do with the "sm10" once you got it, but substitute any command for the "echo".

0
 

Author Closing Comment

by:sk7815
ID: 34956325
Excellent!  Thank you so much!  Actually I wanted to get the email ID of the person searching for his ID in a file and this meets the need!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34956540
I know this is answered and working fine, but if you use a FIND command it will be quicker than a for loop if there are lots of entries as FIND will bring in only the one correct line:

@echo off
for /f "tokens=2 delims=," %%a in ('find "203" ^<yourfile.txt') do set email=%%a
echo The address is %email%
Steve
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:sjklein42
ID: 34956579
nice trick
0
 

Author Comment

by:sk7815
ID: 34956640
thanks Dragon.  I use %1 or the input from the user and getting FIND parameter is invalid.  

@echo off
for /f "tokens=2 delims=," %%a in ('find %1 ^<foo.dat') do set email=%%a
echo The address is %email%

Output:
C:\junk>foo 203
FIND: Parameter format not correct
The address is
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34956661
You probably just need to put " " around it, try "%~1" - the ~ removes any existing " " first.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34956666
You can get more complicated with findstr too, e.g. /b to say beginning a line.
0
 

Author Comment

by:sk7815
ID: 34956886
Thank you, it worked with " " .
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now