power shell + match exactly the first field and capture the second field after separator

0

we have the following file ( files.sum )

the first field is the name of the file

the second field is the seprator

the thired feald is the sum

configuration.txt:2327432786
config.csv:458349573
config.csv1:446433543
mod_binaries.txt:475327527
mod_bin:2358934573875

we want to capture the sum of the file - config.csv

so

$SUM will be the third required value - 458349573

what is the power shell cli that can give the sum values according to the name of the file ?

King David

Posted 2018-10-07T12:40:21.910

Reputation: 405

Answers

0

This can be achieved with the Split command. You can simply loop through each line in files.sim, split with ":" as the delimiter and check if the 0 element in the resulting array equals your file name and if so set $sum to the 1 element of the current array. Here is an example:

$file = 'config.csv'
$seperator = ':'
foreach($line in Get-Content .\files.sum) {
    $NameAndSum = $line.split($seperator)
    if($NameAndSum[0] -eq $file){
        $Sum = $NameAndSum[1]
    }
}
echo $file$seperator$sum

User025

Posted 2018-10-07T12:40:21.910

Reputation: 378