Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to built Batch script to scan for the particular word or phrase in flat file

Posted on 2007-12-05
5
Medium Priority
?
1,014 Views
Last Modified: 2008-02-28
dear experts ,
How to check in flat file though win -batch script that particular word  or phrase  exist.  
- This flat file used  as the input to the script and could be able to return the flag based on the  search.
- my requirement  very much similar functionality  that  "grep" command  do in UNIX environment .  

thanks all in advance ...
0
Comment
Question by:mansur_mca
5 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20417094
You can use find or findstr for this purpose.
0
 
LVL 11

Expert Comment

by:spoxox
ID: 20417181
As you're already familiar with it, you might want to install a windows version of grep from
http://gnuwin32.sourceforge.net/packages/grep.htm

You might need to update your path in order to get it to work:
Start -> Control Panel -> System,
"Advanced" tab
"Environment Variables" button
Select "Path" in System variables, "Edit" button, add semi-colon and directory of your grep installation to the end of the existing text.

0
 
LVL 13

Expert Comment

by:marchent
ID: 20417511
if perl then use attached code.
open(IN);
my @file = <IN>;
close(IN);
 
my $word = 'test'; ## the word to search
 
my $lines_found = grep(m|$word|i, @file); ## number of lines found having word `test` inside the file.

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 20417545
Perl would be overkill for this task, but if it was used, here's a more efficient 1 liner.
perl -ne "print if /some word or phrase/i" file.txt

Open in new window

0
 
LVL 14

Accepted Solution

by:
canali earned 750 total points
ID: 20418851
Only standard dos command.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 
:: look4in.cmd
::  syntax look4in.cmd "word or phase" "c:\program files\myFlatFile.txt"
:::: byBye Gastone Canali 
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Script Start
@Echo Off
cls
SETLOCAL
if %2 ==++ goto :_ERR
IF NOT EXIST %2   goto :_nofile
:: /I case insensitive search
find /I %1 %2
Goto :_END
:_ERR
 echo error: not enought parameters
 Echo syntax look4in.cmd   ^<"phrase"^|"word"^> ^<"file"^>
 Echo syntax look4in.cmd "word or phase" "c:\program files\myFlatFile.txt"
Goto :_END
:_nofile
 echo Error: file not found
Goto :_END
:_END
:: Batch Script End 

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 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