Print line if contains word

后端 未结 2 1712
再見小時候
再見小時候 2021-01-26 17:07

I am trying to make a batch file to execute a few things for me.
Now I do understand a few programming languages, but I haven\'t done much in batch file programming yet.

相关标签:
2条回答
  • 2021-01-26 17:52

    If you simply want to output the matching line with no logic checking, do

    findstr /i "node" "%file1%"
    

    If you want to execute some code if the line is found, use conditional execution.

    findstr /i "node" "%file1%" && (
    
        rem File contained "node".  Do some stuff.
    
    ) || (
    
        rem File did not contain "node".  Do something else.
    
    )
    

    And if you want to use findstr to test for the existence of a string without actually dumping the result to the console, simply add >NUL either before or after the findstr command.

    >NUL findstr /i "node" "%file1%" && (success) || fail
    
    0 讨论(0)
  • 2021-01-26 17:52

    If you are new to scripting, I would avoid batch (shell script) and use PowerShell. PowerShell is far more powerful and flexible.

    get-content 'file.txt' | select-string 'node:(\s+)' | foreach-object {
      $_.Matches[0].Groups[1].Value
    }
    

    This command would output the first substring match (from inside parentheses in regular expression) from each line.

    0 讨论(0)
提交回复
热议问题