Solved

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

Posted on 2011-02-22
9
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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