Solved

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

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

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…

756 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