Powershell从路径名中选择字符串?

时间:2016-11-02 19:38:56

标签: powershell csv

我有一个带有目录路径的CSV文件,我需要从中提取一些信息来创建一个日志文件名。不知道怎么做?    Select-String

CSV文件看起来像这样

User,Computer,Directory
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Program\IT\var\post2
john.doe,CAD-12,C:\Cimatron\CimatronE11_64bit\Data\templates
john.doe,CAD-12,C:\Program Files\CGTech\VERICUT 7.3.3\library
john.doe,CAD-12,C:\Program Files\CGTech\VERICUT 7.4\library

我知道我必须导入CSV文件,这就是我所拥有的。

$UsersCSV = import-csv C:\test.csv
foreach($Computer in $UsersCSV)
{$Logfile = Select-String "not sure how to search the string for what I want"
$LogFile
}

需要Select-String的帮助。以下是我希望最终得到的内容或最简单的$Logfile唯一名称。

CimE11_post
CimE11_post2
CimE11_templates
VERI_7.3.3_lib
VERI_7.4_lib

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

TessellatingHeckler在对该问题的评论中建议使用实用解决方案:使用合法字符替换输入路径中的\:个实例文件名,用于表示作为文件名的每个路径:

Import-Csv C:\test.csv | % {  # % is a built-in alias for ForEach-Object
  $Logfile = ($_.Directory -replace '[\\: ]+', '_') + '.log'
}

这会产生以下文件名:

C_Cimatron_CimatronE11_64bit_Program_IT_var_post.log
C_Cimatron_CimatronE11_64bit_Program_IT_var_post2.log
C_Cimatron_CimatronE11_64bit_Data_templates.log
C_Program_Files_CGTech_VERICUT_7.3.3_library.log
C_Program_Files_CGTech_VERICUT_7.4_library.log

正如评论中所述,您想要的文件名背后没有明确的算法逻辑

这是一个产生所需文件名的解决方案,尽量保持通用 这可能不值得在现实生活中做,但也许它可以启发现实生活中的解决方案:

# Helper function that takes a directory path and converts it to a log filename
# representing that dir.
function pathToLogFilename([string] $path) {

  # Ordered hashtable defining regex-based transformations (replacements).
  # Entries are processed in order, and processing stops as soon as 
  # a match is found and the transformation has been performed.
  # Entry format: <regex> = <replacement>, to be passed to the -replace operator.
  $ohtTansform = [ordered] @{
    '^(.{3}).+?(E\d+).*$' = '$1$2' # 'CimatronE11_64bit' -> 'CimE11', for instance. 
    '^(.{4}).+? (\d.*)$' = '$1_$2' # 'VERICUT 7.3.3' -> 'VER_7.3.3', for instance
    '^(lib)rary$' = '$1'           # 'library' -> 'lib'
    '^(post.*|templates)$' = '$&'  #  preserve, if prefixed with 'post' or equal to 'templates' 
    '.+' = ''                      #  remove all other tokens
  }

  $logFilename = ''; $i = 0
  # Split the .Directory path into tokens (path components) and synthesize
  # the log filename from a subset of the tokens, with transformations applied.
  foreach($token in ($path -split '\\')) {
    # Apply transformation.
    $transformedToken = $token
    foreach($regex in $ohtTansform.Keys) {
      if ($token -match $regex) {
        $transformedToken = $token -replace $regex, $ohtTansform[$regex]
        break
      }
    }
    # Add transformed token to filename, separated with "_".
    if ($transformedToken) {
      $logFilename += $(if ($i++) { '_' } else { '' }) + $transformedToken 
    }
  }
  # Output the synthesized filename.
  $logFilename

}


Import-Csv C:\test.csv | % {
  # Call the helper function to transform the directory path to a log filename.
  $LogFile = pathToLogFilename $_.Directory
}

答案 1 :(得分:-1)

-replace是你的朋友,下面是我如何从你给出的场景中得到你想要的字符串的一个例子。

希望这会有所帮助,祝你好运。

$ file = import-csv $ filename

foreach($ file.directory中的$ dir){ if($ dir -match&#34; VERI&#34;){ (((($ dir).replace(&#39; C:\ Program Files \ CGTech \&#39;,&#39;&#39;))。replace(&#39; rary&#39;,& #39;&#39;))。替换(&#39; CUT&#39;,&#39; &#39;))。替换(&#39; \&#39;,&# 39; &#39;) }

if($ dir -match&#34; Cima&#34;){ (((($ DIR).replace(&#39; C:\ Cimatron的\&#39;&#39;&#39))代替(&#39; ATRON&#39;,&#39; &#39;。))代替(&#39; 64 \数据\&#39;&#39; &#39;。))代替(&#39; 64位\程序\ IT \ VAR \&#39;&#39;&#39;) }

}