Home > database >  How to reproduce my loop in bash with powershell?
How to reproduce my loop in bash with powershell?

Time:09-16

I've this type of file :

foo/foo,foo2/foo2,foo3/foo3

And I Would like this output :

foo/foo
foo1/foo1
foo2/foo2

With bash I'm able to do that (even if I sure that is not the cleanest way, but it works ) :

#!/bin/bash

numdel=3

while IFS="," read line
do
        for i in $(seq 1 $numdel)
        do
                echo $line | cut -d',' -f$i

        done
done < CSV.csv

The output is :

foo/foo
foo1/foo1
foo2/foo2

I try to reproduce the same but in powershell and I can't... There is someone to show me how to do that with powershell ?

Thanks a lot !

CodePudding user response:

$files = 'foo/foo,foo2/foo2,foo3/foo3'

$files.split(',')

enter image description here

So for input from a file you would need to use


$files = Get-Content $filePath

If you had in excess of one file you would need to create an object having them and then loop through them:


$items = Get-ChildItem -Path 'c:\some\target\path' -filter '*.csv'

ForEach ($item in $items) {

 $files = Get-Content -Path $item.FullName
 $output = $files.split(',')

 return $output

}

You could then redirect that output to a file using

> - create a new file
>> - append to a file

Or using

Set-Content
  • Related