Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
搜索CSV文件以查找特定值,而不使用Powershell指定列名_Powershell_Csv - Fatal编程技术网

搜索CSV文件以查找特定值,而不使用Powershell指定列名

搜索CSV文件以查找特定值,而不使用Powershell指定列名,powershell,csv,Powershell,Csv,是否有一种方法可以逐行搜索.CSV文件以查找特定值,而无需指定要搜索的列名,是否希望在多个.CSV文件上运行此脚本,因此指定列名不是我的选项 foreach ($row in $csvFile){ if ($row -eq/-contains $StringIWantToFind) { #do something with the string here } } PowerShell代码示例: foreach ($row in $csvFile){ if

是否有一种方法可以逐行搜索.CSV文件以查找特定值,而无需指定要搜索的列名,是否希望在多个.CSV文件上运行此脚本,因此指定列名不是我的选项

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
PowerShell代码示例:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}

如果不关心(子)字符串在哪个列中,可以使用通配符匹配:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
$row -like "*$StringIWantToFind*"
或正则表达式匹配:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
$row -match $StringIWantToFind
如果您想将值用于某个对象,则后者可能是更好的选择,因为它通过自动变量
$matches
:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
$StringIWantToFind = 'something (captured group) or other'

foreach ($row in $csvFile) {
    if ($row -match $StringIWantToFind) {
        # do something with $matches[0] (full match) or $matches[1] (captured
        # group) here
    }
}

最快的方法是使用
选择字符串
,如下所示:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
Select-String your_file.txt -Pattern 'string to find' -SimpleMatch
如果要处理结果,可以按如下方式提取匹配线:

foreach ($row in $csvFile){
    if ($row -eq/-contains $StringIWantToFind) {
        #do something with the string here
    }
}
Select-String your_file.txt -Pattern 'xx' -SimpleMatch | Select -ExpandProperty line | % {
  # your processing here using $_
}