Solved

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

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

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

758 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

18 Experts available now in Live!

Get 1:1 Help Now