www.hardwarezone.com.sg

www.hardwarezone.com.sg (/)
-   The Programmers Den (https://forums.hardwarezone.com.sg/programmers-den-296/)
-   -   Scripting to run command line tools (https://forums.hardwarezone.com.sg/programmers-den-296/scripting-run-command-line-tools-5864885.html)

u0206397 11-07-2018 11:58 AM

Scripting to run command line tools
 
I have a CSV file containing data used to invoke a command line tool.

Example of CSV:

PHP Code:

name,value
john
,1
robert
,2
michael
,3
william
,4
patricia
,5
jennifer
,6
linda
,7
elizabeth
,


The command line would look like this:

PHP Code:

process_data.exe -name "john" -value 1 -"line_0001.dat"

... repeat ...

process_data.exe -name "elizabeth" -value 8 -"line_0008.dat" 

What is the easiest/elegant way to repeatedly execute a Windows command line program and vary the input parameters and output file name, hopefully without installing any complicated run time or interpreter environment?

Normal batch file (.bat and .cmd) can't seem to parse and loop a CSV file easily.

Perl? (interpolate the command line string with parameters and use backtick operator? )

Code:

`process_data.exe -name $name -value $value -o $outfile`
PowerShell?

Code:

Start-Process '$EXE -name "$name" -value "$VALUE" -o "$OUTFILE"'
Python?
Code:

os.system('process_data.exe -name "' + name + '" -value ' + value + ' -o "' + outfile + '"')
Not on Linux, so discounting Bash scripting.

davidktw 11-07-2018 12:15 PM

You can try activestate perl or gawk on windows

Code:

perl -ne '$a=split /,/;' < input.csv
For perl, you can do this
Code:

perl -ne '@a=split /,/; system("process_data", "-name", $a[0], "-value", "-o", sprintf("line_%04d.dat", $a[1])) unless ($a[0] != "name" and $a[1] != "value")'

u0206397 12-07-2018 10:45 AM

Giving ActivePerl a spin today.

I notice that the way PPM names their package is slightly different from CPAN. Using a hypen ( - ) instead of a double colon ( :: ). E.g. Data-Dumper (PPM) instead of Data:: Dumper (CPAN).

Not sure if this means any changes required in the code. Will try. :s11:

davidktw 12-07-2018 10:54 AM

Quote:

Originally Posted by u0206397 (Post 115439879)
Giving ActivePerl a spin today.

I notice that the way PPM names their package is slightly different from CPAN. Using a hypen ( - ) instead of a double colon ( :: ). E.g. Data-Dumper (PPM) instead of Data:: Dumper (CPAN).

Not sure if this means any changes required in the code. Will try. :s11:

That is the packaging name. The code still uses :: as the namespace separator.


All times are GMT +8. The time now is 08:29 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Copyright © SPH Magazines Pte Ltd. All rights reserved.