views:

123

answers:

4

I need to make a script that automates the following:

  1. Read comma or tab separated values from the input file
  2. Construct a command from them and run it
  3. Get the command output, looks for substrings in it and update the log file based on the existence of a substring

I do this in Windows Server 2008, so I can do this in CMD or PowerShell but I am not sure they provide such possibilities. I can try Perl or C#, but I want to try a minimalistic approach first.

A: 

I would recommend going with Python (or Perl if you swing that way). These are very minimal tools to have to install on a machine and add all the functionality you need.

The string handling you describe is unpleasant in any shell (Bash included) unless you are using sed or awk... and that just gets esoteric. In the end you'll retain more hair if you go straight to a scripting language first.

Chadwick
+1  A: 

Minimalistic as far as coding - Perl

Minimalistic as far as installing new software - PowerShell (IIRS W.S.2008 included that?)

DVK
A: 

Perl was called into existence to quickly solve these kind of tasks. It should not take more 20 lines for this particular problem.

It is also really easy to install:

  1. Download ActivePerl (17.7 MB, Perl 5.10.)
  2. Run the installer.
Peter Mortensen
+1  A: 

So many answers, and none providing a solution that would meet the requirements...

You didn't say what are the conditions to be checked against each CSV row, and what the CSV would be like, and what the log would be like - so I made it all up... Here's an example in BATCH:

@echo off
set csvfile=input.csv
set logfile=output.log


for /F "tokens=1,2,3 delims=," %%a in (%csvfile%) do call :processline "%%a" "%%b" "%%c"
exit /B 0

:processline
    set param=%~3
    set check=%param:um=%
    rem if they are not equal - substring 'um' exists in it.
    if not "$%check%" == "$%param%" (
        rem this passes all params to output.
        rem I'm not calling echo directly here, because there might be symbols, that will confuse CMD at the end of the %*.
        call :output %*>> %logfile%
    )
    exit /B 0

:output
    set colA=%~1
    set colB=%~2
    set colC=%~3
    rem output to log
    echo [%DATE% %TIME%] (%colB%) %colA% %colC%.
    exit /B 0

Here's the example input file that I tested it with:

foo,1,dum
bar,3,dim
baz,15,dirum

And here's the resulting log messages:

[2009-10-14 14:57:35.87] (1) foo dum.
[2009-10-14 14:57:35.89] (15) baz dirum.

I hope this shows clearly, that BATCH is not nasty nor it is hard to use. :P

If you have further question about BATCH - don't be shy, post them all on SO. ;)

Paulius Maruška