Solved

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

Posted on 2011-02-22
9
306 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
nice trick
0
 

Author Comment

by:sk7815
Comment Utility
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
Comment Utility
You probably just need to put " " around it, try "%~1" - the ~ removes any existing " " first.

Steve
0
 
LVL 43

Expert Comment

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

Author Comment

by:sk7815
Comment Utility
Thank you, it worked with " " .
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

11 Experts available now in Live!

Get 1:1 Help Now