Solved

Findstr batch file - need help

Posted on 2011-09-26
13
559 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
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
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
 
LVL 1

Author Comment

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

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 51

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 51

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

20 Experts available now in Live!

Get 1:1 Help Now