string 在powershell中的分隔符之后/之前切断字符串中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5203730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cut off text in string after/before seperator in powershell
提问by DemonWareXT
Ok so here is what I'm trying to do: I want to make a little powershell script that takes each file of my music library and then does a hash sum of it and writes that into a file like so:
好的,这就是我想要做的:我想制作一个小的 powershell 脚本,它获取我的音乐库中的每个文件,然后对其进行哈希和并将其写入文件中,如下所示:
test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198
测试.txt; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198
Now when I start the script I need it to first compare my music library whith the already existing values but for this I just want to cut off everything after the ; so that it can compare filename against filename (or filepath)... but I'm stumped at how to do that, tried replacing with $name = $name -replace ";*","" no go...
现在,当我启动脚本时,我需要它首先将我的音乐库与已经存在的值进行比较,但为此我只想切断 ; 之后的所有内容。以便它可以将文件名与文件名(或文件路径)进行比较......但我对如何做到这一点感到困惑,尝试用 $name = $name -replace ";*","" 替换...
tried to filter... don't know how D:
试图过滤...不知道如何 D:
I'd really appreciate help.
我真的很感激帮助。
Also if you think I'm useing the wrong coding language, tell me and what would be better, it's just I only ever used C and powershell
另外,如果您认为我使用了错误的编码语言,请告诉我什么会更好,只是我只使用过 C 和 powershell
回答by VVS
$pos = $name.IndexOf(";")
$leftPart = $name.Substring(0, $pos)
$rightPart = $name.Substring($pos+1)
Internally, PowerShell uses the String class.
在内部,PowerShell 使用String 类。
回答by Sarang
$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"
$text.split(';')[1].split(' ')
回答by hdoghmen
You can use a Split
:
您可以使用Split
:
$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"
$separator = ";" # you can put many separator like this "; : ,"
$parts = $text.split($separator)
echo $parts[0] # return test.txt
echo $parts[1] # return the part after the separator
回答by David
This does work for a specific delimiter for a specific amount of characters between the delimiter. I had many issues attempting to use this in a for each loop where the position changed but the delimiter was the same. For example I was using the backslash as the delimiter and wanted to only use everything to the right of the backslash. The issue was that once the position was defined (71 characters from the beginning) it would use $pos as 71 every time regardless of where the delimiter actually was in the script. I found another method of using a delimiter and .split to break things up then used the split variable to call the sections For instance the first section was $variable[0] and the second section was $variable[1].
这确实适用于分隔符之间特定数量字符的特定分隔符。我在尝试在位置发生变化但分隔符相同的每个循环中使用它时遇到了很多问题。例如,我使用反斜杠作为分隔符,并且只想使用反斜杠右侧的所有内容。问题是,一旦定义了位置(从开头开始的 71 个字符),无论分隔符在脚本中的实际位置如何,它每次都会使用 $pos 作为 71。我找到了另一种使用分隔符和 .split 来分解事物的方法,然后使用 split 变量来调用部分。例如,第一部分是 $variable[0],第二部分是 $variable[1]。
回答by Clarius
I had a dir full of files including some that were named invoice no-product no.pdf and wanted to sort these by product no, so...
我有一个充满文件的目录,包括一些名为发票无产品编号.pdf 的文件,并希望按产品编号对这些文件进行排序,所以...
get-childitem *.pdf | sort-object -property @{expression={$\_.name.substring($\_.name.indexof("-")+1)}}
Note that in the absence of a -
this sorts by $_.name
请注意,在没有-
这种排序方式的情况下$_.name