Solved

Findstr batch file - need help

Posted on 2011-09-26
13
586 Views
Last Modified: 2012-06-27
I need a batch file that will extract certain strings in a csv file for multiple servers.  The path would be c:\apps\*.csv.  I want the extracted data into a output.txt file.  I just need lines containing the strings.

I would like two input files...
string.txt = phrases containing strings to search for
server.txt = servers to check in a certain path

I will reward points asap!

Thanks!
0
Comment
Question by:mystikal1000
[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
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36602297
If I understand right, you need to search in file "server.txt" for strings that are in "string.txt" and send output to "output.txt".

With findstr you would do this:
findstr /g:string.txt server.txt > output.txt

Open in new window


When string.txt contains:
server1
server3
server8

Open in new window

And server.txt contains:
test this server 1
test that server1
test every server3
test other server5

Open in new window


output.txt would contain:
test that server1
test every server3

Open in new window



0
 
LVL 1

Author Comment

by:mystikal1000
ID: 36602399
No I need to search c:\apps\*.csv files that includes strings from string.txt.  I need it to search for each server in the servers.txt file.

%server%\c$\appse\*.csv

Hope it makes sense.
0
 
LVL 2

Expert Comment

by:leighyoung
ID: 36602605
vbscript ok or does it require Batch?
0
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!

 
LVL 1

Author Comment

by:mystikal1000
ID: 36642040
I like batch, vbscript is fine too.  Love to get some solutions soon!
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 125 total points
ID: 36708296
So server.txt contains your servers to check, OK.

This would do just fine:

for /f %f in (server.txt) do @findstr /g:string.txt \\%f\c$\apps\*.csv >> output.txt

Open in new window


You output.txt looks like this:

\\<server>\c$\data\<somefile>.csv:<line matches string from string.txt>
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 375 total points
ID: 36710813
As a batch file this might look something like the following:

@echo off

for /f %%a in (server.txt) do (
   findstr /g:string.txt \\%%a\c$\apps\*.csv >>output.txt
)

Open in new window

0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 36816478
@mystikal1000

I'd ask that you consider closing this question slightly differently.  If 36710813 was the solution you used that's fine, but I feel like gerwinjansen should have at least gotten an assist and a chuck of the points. His post 36708296 is essentially the exact same thing, only he delivered the syntax you could use right from a command prompt rather than in a BAT file.  paultomasi clarified that to use it in a BAT file you simply have to change the FOR loop variables to be referenced with %% instead of %.

Ultimately it's your call, but in reading this question I felt like gerwinjansen should have gotten some recognition for his contribution to the ultimate solution.

~bp
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 36820885
How do I reward points to both?
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 36826855
Use the request Attention link near your original question and ask a moderator to reopen the question so you award points differently.  Here are a couple of useful links from the Help area.

http://www.experts-exchange.com/help.jsp#hs=29&hi=409

http://www.experts-exchange.com/help.jsp#hs=29&hi=407

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36890280
It has to bourne in mind mystikal1000 was quite specific in stating: "I need a batch file that will...".

Personally, I'd vote for leaving the award as it is and marking this one up as a lesson to be learned: Read the question!

As harsh as that may appear to sound, it's the difference between delivering and NOT delivering what was asked for.

I can provide you with a long list of experts who would have given almost identical (if not exactly the same) code as that given in the solution. In that case, one would have to consider who answered first.

It is astute of billprew to draw attention to this - perhaps he should consider becoming an EE Moderator.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36894800
You gotta be kiddin' - the last ferrari cost me two t-shirts !!
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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