我有一个文本文件,它包含一组UNIX路径格式的字符串。我需要从每个字符串中提取由'/‘分隔的最后一个字符串。
/Public/Home/Call/Reports/Weekly Template/C555 Weekly Report - Monday
/Public/Home/Call/Reports/Weekly Template/C567 Weekly Report - Monday
/Public/Home/Call/Reports/Weekly Template/C555 Weekly Report - Tuesday
/Public/Home/Claim Repo/Reports/Weekly Set/@Weekly Report - $100 for Monday
/Public/Home/Claim Report/Reports/Weekly Not Set/ Weekly Report - Monday我需要在Windows上运行这些程序,因此需要在PowerShell或批处理上运行。我需要帮助处理脚本,该脚本也允许空格和特殊字符,并且可以读取.TXT文件并以以下格式输出完整的字符串:
/Public/Home/Call/Reports/Weekly Template C555 Weekly Report - Monday
/Public/Home/Call/Reports/Weekly Template C567 Weekly Report - Monday
/Public/Home/Call/Reports/Weekly Template C555 Weekly Report - Tuesday
/Public/Home/Claim Repo/Reports/Weekly Set @Weekly Report - $100 for Monday
/Public/Home/Claim Report/Reports/Weekly Not Set Weekly Report - Monday我在另一个答案中找到了一个解决办法:
http://stackoverflow.com/questions/20004597/extracting-string-after-last-instance-of-delimiter-in-a-batch-file它的代码是:
for %%a in (%param:\= %) do set lastDir=%%a
:nextParam
set "param=%~1"
if not defined param goto endParams
for %%a in (%param:\= %) do set lastDir=%%a
echo Last dir: %lastDir%
shift
goto nextParam
:endParams或者,以一种更简单的方式(没有空格限制):
:nextParam
if "%~1" equ "" goto endParams
echo Last dir: %~N1
shift
goto nextParam
:endParams但是,我也想满足字符串中的空格和特殊字符的需要,而上面没有这样做。
发布于 2014-09-23 04:04:07
如果工作的话,试试这个
(get-content foo.txt) -replace '.*/'说明:'.*/‘:把所有的字符吃到字符串中的最后一个位置,然后回溯直到找到第一个'/’。
|a|b|/|c|/|d|
---------|-->
| <-
|<--发布于 2014-09-23 01:32:03
在PowerShell中,您可以这样做:
Get-Content foo.txt | Where {$_ -match '/'} | Foreach {$_.Substring($_.LastIndexOf('/')+1)}管道的Where阶段是过滤空行和不指定路径的行。
https://stackoverflow.com/questions/25985268
复制相似问题