区分数据权力

时间:2017-02-23 03:12:19

标签: powershell

所以我们有很多非常零星的数据,

的示例:

> Col 1               col 2            col 3 
> 0123456             35 StreetName    2134  
> 22 Street Name      0123456          2121 
> 2121                0123456          32 street name
> 32 streetname 1212  01474412312      1212

在Power shell中,区分这些数据位的最佳方法是什么?

尝试将数据放入正确的col中,因此所有地址均为1,电话号码为2,邮政编码为3。

数据来自,

$s = "0123456" (how would i know that this belongs in col 3)
$s = "35 street name" (how would i know that this belongs in col 1)
$s = "1212" (how would i know that this belongs in col 2)
$s = "32 Street Name 3212" (how would i know that this belongs in col 1)
$s = "0(3) 6363727" (how would i know that this belongs in col 3)

1 个答案:

答案 0 :(得分:2)

使用正则表达式测试传入的字符串:

switch -Regex ($string) {
    '^\d{1,4}$' {
    # 1-4 digits and nothing else
        $col = 'zip' # or 1 depending on your data format
    }
    '\d{6}' {
    # 6 digits anywhere inside
        $col = 'phone' # or 3 depending on your data format
    }
    default {
        $col = 'address' # or 2 depending on your data format
    }
}

$row[$col] = $string # or $row.$col = $string depending on your data format