Solved

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

Posted on 2011-02-22
9
345 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

636 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