1

OK guys, I have 24 subfolders, named Angle1, Angle2, etc up to Angle24, each with a file called output.txt. Note that these folders have subfolders which I want to ignore.

Is there a way to loop through these folders and take the 9th column of data (not 9th column by character, the data can be several characters long but each column is separated by a space). And put each 9th column from all output.txt files into a new file called total.txt in parent directory? So I will ultimately a file called total.txt with 24 columns of data, where the first column corresponds to column 9 of output.txt in Angle1, etc.

I need this to be done in powershell.

  • This appears to be a duplicate of superuser.com/questions/915465/…. Can you explain how this question is different? Generally, if you aren't getting an answer that meets your needs, you should clarify the original question. – fixer1234 May 16 '15 at 18:10
  • It is different because in the other I specified linux was fine, here I need the answer to be in powershell. I cannot figure out cygwin – Jackson Hart May 16 '15 at 18:11
  • The other question already includes Powershell. I just added the tag to make it more explicit, which will also refresh it in the active questions list. If your question is going to attract an answer, the original question should do it. Duplicate questions clutter the site. – fixer1234 May 16 '15 at 18:18
0

powershell command-line, create directory, run at D:\Data Set\:

powershell 1..24^|%{md ('Angle'+$_)}

powershell command-line, create test data:

powershell $i=0;$d='D:\Data Set\';$r=New-Object -T Random;1..24^|%{++$i;1..(random(11..17))^|%{$s='';1..20^|%{$rn=$r.Next();$s+=''+$rn+' '};$s.Trim();$s^|ac ($d+'Angle'+$i+'\output.txt')}}
  • $r=New-Object -T Random - Create Random object
  • $s.Trim() - delete space left and rigth side of string

Run powershell file:

powershell .\PasteD.ps1

PasteD.ps1, get and write column:

$d='D:\Data Set\' # work directory
$FileNum=24       # part path and filename
$Coln=9           # - 9th column
$LineMax=0        # init variable maximum line at text file
$dlm=' '          # delimiter

$i=0;1..$FileNum|%{++$i;$LineMax=[Math]::Max($LineMax,(gc ($d+'Angle'+$i+'\output.txt')|Measure).Count)}
# (gc <file name>|Measure).Count) - get line count at text file
# [Math]::Max - get Maximum line count at all file
# 1..$FileNum - cycle at Angle1, Angle2, etc up to Angle24 directory

# way init array: 

#$arS =,''*$LineMax
#$arS =@('')*$LineMax
#$arS=[array]::CreateInstance('String', $LineMax)
#$arS=New-Object 'object[]' $LineMax
[string[]]$arS=@('')*$LineMax

For($i=0; $i -lt $FileNum-1; $i++) {
$f=gc ($d+'Angle'+($i+1)+'\output.txt') # get content file in Angle1..Angle23 dir

  For($j=0; $j -lt $f.length; $j++) {
  $arS[$j]=$arS[$j] + ($f[$j]-split' ')[$Coln-1] + $dlm # add value and delimiter
  }
  For($j=$f.length; $j -lt $LineMax; $j++) {
  $arS[$j]=$arS[$j] + $dlm # add delimiter and empty value
  }
}

$f=gc ($d+'Angle'+($FileNum)+'\output.txt') # get content file in Angle24 dir

  For($j=0; $j -lt $f.length; $j++) {
  $arS[$j]=$arS[$j] + ($f[$j]-split' ')[$Coln-1] # after last column not add delimiter
  }

$arS|ac($d+'\total.txt') # save string array to result file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.