首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从路径名中选择字符串?

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

Stack Overflow用户
提问于 2016-11-02 19:38:56
回答 2查看 1K关注 0票数 0

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

CSV文件如下所示

代码语言:javascript
运行
复制
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文件,这是我所拥有的。

代码语言:javascript
运行
复制
$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一个唯一的名字。

代码语言:javascript
运行
复制
CimE11_post
CimE11_post2
CimE11_templates
VERI_7.3.3_lib
VERI_7.4_lib

谢谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-04 02:04:34

TessellatingHeckler在关于以下问题的评论中提出了一个实用解决方案:将输入路径中的\:实例替换为文件名中合法的字符,以获得作为文件名工作的每个路径的表示形式。

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

这将产生以下文件名:

代码语言:javascript
运行
复制
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

正如注释中所述,在您说要的文件名后面没有明确的算法逻辑。

下面是一个生成所需文件名的解决方案,试图保持尽可能的泛型。

这在现实生活中可能不值得去做,但也许它能激发出一种真实的解决方案:

代码语言:javascript
运行
复制
# 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
}
票数 0
EN

Stack Overflow用户

发布于 2016-11-02 23:37:54

-replace是您的朋友,下面是我如何从您提供的场景中获取您想要的字符串的示例。

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

$file =导入-csv $filename

foreach($dir in $file.directory){ if($dir -match "VERI"){ ($dir).replace(‘C:\Program\CGTech\ '')).replace('rary','') .replace(’$dir ',‘’).replace(‘\’,‘’)}

($dir -match "Cima"){ ($dir).replace(‘C:\Cimatron\,'')).replace('atron','') ).replace(’64位\Data‘,’‘).replace(’64位\Program\IT\var‘,’‘)}

}

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40388371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档