tags:

views:

62

answers:

2

I just had to produce a long xml sequence for some testing purpose, a lot of elements like <hour>2009.10.30.00</hour>.

This made me drop into a linux shell and just run

for day in $(seq -w 1 30) ; do  
  for hour in $(seq -w 0 23) ; 
    do echo "<hour>2009.10.$day.$hour</hour>" ; 
  done ; 
done >out

How would I do the same in powershell on windows ?

+2  A: 

Pretty similar...

$(foreach ($day in 1..30) {
 foreach ($hour in 0..23) {
  "<hour>2009.10.$day.$hour</hour>"
 }
}) > tmp.txt

Added file redirection. If you are familiar with bash the syntax should be pretty intuitive.

Paolo Tedesco
How would I redirect that output to a file ?
Anonym
+2  A: 

If I were scripting I would probably go with orsogufo's approach for readability. But if I were typing this at the console interactively I would use a pipeline approach - less typing and it fits on a single line e.g.:

1..30 | %{$day=$_;0..23} | %{"<hour>2009.10.$day.$_</hour>"} > tmp.txt
Keith Hill
Using a pipeline with the Foreach-Object cmdlet (%) is more idiomatic of PowerShell than using a 'foreach' statement. In a script, replace the '%' with 'foreach' if you want, and break in to multiple lines.
Jay Bazuzi