How to overwrite powershell code in bash

I was instructed to rewrite it in bash. But while most powershell are easy to read, I just don’t understand what this block actually does !!? Any ideas?

First you need a file that is sorted by keyword, perhaps this is important!

Thanks for any ideas!

foreach ($line in $sfile)
{
  $row = $line.split('|');

  if (-not $ops[$row[1]]) {
    $ops[$row[1]] = 0;
  }

  if ($row[4] -eq '0') {
    $ops[$row[1]]++;
  }

  if ($row[4] -eq '1') {
    $ops[$row[1]]--;
  }

  #write-host $line $ops[$row[1]];

  $prevrow = $row;
}
+3
source share
3 answers

Perhaps a little refactoring will help:

foreach ($line in $sfile) 
{ 
  # $row is an array of fields on this line that were separated by '|'
  $row = $line.split('|'); 
  $key = $row[1]
  $interestingCol = $row[4]

  # Initialize $ops entry for key if it doesn't 
  # exist (or if key does exist and the value is 0, $null or $false)
  if (-not $ops[$key]) { 
    $ops[$key] = 0; 
  } 

  if ($interestingCol -eq '0') { 
    $ops[$key]++; 
  } 
  elseif ($interestingCol -eq '1') { 
    $ops[$key]--; 
  } 

  #write-host $line $ops[$key]; 

  # This appears to be dead code - unless it is used later
  $prevrow = $row; 
} 
+3
source

'|' charater . , $row $ops var. , , , , , $ops ifs-, , 5- $row , - , , .

0

About:

#!/bin/bash
saveIFS=$IFS
while read -r line
do
    IFS='|'
    row=($line)

    # I don't know whether this is intended to test for existence or a boolean value
    if [[ ! ${ops[${row[1]}] ]]
    then
        ops[${row[1]}]=0
    fi

    if (( ${row[4]} == 0 ))
    then
        (( ops[${row[1]}]++ ))
    fi


    if (( ${row[4]} == 1 ))
    then
        (( ops[${row[1]}]-- ))
    fi

    # commented out
    # echo "$line ${ops[${row[1]}]}

    prevrow=$row
done < "$sfile"
0
source

Source: https://habr.com/ru/post/1761758/


All Articles