![]() |
Script for split file
I need script that will "ask" me number of workers and then split input file equally into worktodo.txt file ( and if possible add "Worker line") ( in this case 3 workers)
[QUOTE]example input.txt PRP=1,2,3 PRP=1,2,4 PRP=1,2,5 output [Worker #1] PRP=1,2,3 [Worker #2] PRP=1,2,4 [Worker #3] PRP=1,2,5 [/QUOTE] Thanks |
[QUOTE=pepi37;605586]I need script that will "ask" me number of workers and then split input file equally into worktodo.txt file ( and if possible add "Worker line") ( in this case 3 workers)
[/QUOTE] Windows or Linux? What is your preferred (scripting) language? |
In [C]bash[/C]:
[CODE]if [ $# -ne 3 ]; then echo 'Usage: bash splitter.sh [in file] [out file] [number of workers]'; exit -1; fi; if ls "$1."* 1> /dev/null 2>&1; then echo 'Temporary files could not be created.'; exit -2; fi; range=$(seq 1 $3); if [ $? -ne 0 ]; then echo 'The worker count was not numerical.'; exit -3; fi; for w in $range; do echo "[Worker #$w]" >> "$1.tmp"; done; cat "$1" >> "$1.tmp"; split -n r/$3 "$1.tmp" "$1.s"; cat "$1.s"* > "$2"; rm "$1."*;[/CODE] |
[QUOTE=paulunderwood;605587]Windows or Linux? What is your preferred (scripting) language?[/QUOTE]
Whatever I can run under Linux 😀 Language, everyone you like it. I will adopt Thanks |
What about this tool? [url]https://www.mersenne.ca/balance.php[/url]
|
[QUOTE=Uncwilly;605597]What about this tool? [url]https://www.mersenne.ca/balance.php[/url][/QUOTE]
At first sight look perfect! |
[QUOTE=Uncwilly;605597]What about this tool? [URL]https://www.mersenne.ca/balance.php[/URL][/QUOTE]
But doesnot work on PRP tests... |
[QUOTE=kruoli;605595]In [C]bash[/C]:
[CODE]if [ $# -ne 3 ]; then echo 'Usage: bash splitter.sh [in file] [out file] [number of workers]'; exit -1; fi; if ls "$1."* 1> /dev/null 2>&1; then echo 'Temporary files could not be created.'; exit -2; fi; range=$(seq 1 $3); if [ $? -ne 0 ]; then echo 'The worker count was not numerical.'; exit -3; fi; for w in $range; do echo "[Worker #$w]" >> "$1.tmp"; done; cat "$1" >> "$1.tmp"; split -n r/$3 "$1.tmp" "$1.s"; cat "$1.s"* > "$2"; rm "$1."*;[/CODE][/QUOTE] Work perfect. Now I will see can I adopt it to Win Thanks! |
1 Attachment(s)
[QUOTE=pepi37;605627]But doesnot work on PRP tests...[/QUOTE]
I just tried it with some made up PRP lines and it recognized them, sorted them, and attempted to balance them. |
bash
[CODE]N=3 for i in `seq 1 $N`; do echo '[Worker #'$i']'; awk 'a++%'$N'=='$i'-1' input.txt done > output.txt [/CODE] |
Here is a perl script. usage: [C]perl myscript.pl input_file worker_count > output_file[/C]
[CODE]open(fh,"<",$ARGV[0]); $count=0; while(<fh>){$count++;} close(fh); open(fh,"<",$ARGV[0]); $per_worker=int($count/$ARGV[1]); $extras=$count%$ARGV[1]; $worker_blurb="[Worker \#"; $worker=0; while(<fh>){ $line=$_; $count++; if($count>$per_worker){ $count=0; print("\n"); $worker++; print($worker_blurb.$worker."]\n"); if($extras==0){$per_worker--;} $extras--; } print($line); } close(fh); [/CODE] |
[QUOTE=pepi37;605586]I need script that will "ask" me number of workers and then split input file equally into worktodo.txt file ( and if possible add "Worker line") ( in this case 3 workers)
Thanks[/QUOTE] Like [URL="https://www.mersenne.ca/balance.php"]this[/URL] does? Maybe you can access the source. |
[QUOTE=Batalov;605630]bash
[CODE]N=3 for i in `seq 1 $N`; do echo '[Worker #'$i']'; awk 'a++%'$N'=='$i'-1' input.txt done > output.txt [/CODE][/QUOTE] Perfect! I made two scripts, one for 8 worker one for 12 worker Thanks! |
All times are UTC. The time now is 06:08. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.