Solved

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

Posted on 2007-12-05
5
1,008 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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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…

825 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