Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,013 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
[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
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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

715 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