Select-string issue (trying to get rid of file name and line #)

Welcome Forums General PowerShell Q&A Select-string issue (trying to get rid of file name and line #)

Viewing 5 reply threads
  • Author
    Posts
    • #249977
      Participant
      Topics: 8
      Replies: 9
      Points: 158
      Rank: Participant

      I have csv file likes this

      abc.csv

      HostName,Processor,memory,HD_size
      PC101,4,16G,256G
      SRV101,6,64,4096G
      SRV102,4,16,4096G
      SRV103,6,32,4096G
      SRV104,6,64,4096G
      PC102,4,16G,256G
      PC103,4,6G,128G
      PC104,2,4G,64G

      Select-string -Pattern PC101 -path .\abc.csv

      abc.csv:2:PC101,4,16G,256G

      How can I get rid of filename and line#?

      I prefer output likes this:

      PC101,4,16G,256G

      Thanks,

      Sam

    • #249989
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      Don’t use Select-String? Why are you using it when you could simply do this.

      Import-CSV abc.csv | where hostname -eq 'PC101'

    • #250025
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,323
      Helping Hand
      Rank: Community Hero

      This will work…

      Thank you.

    • #250097
      Participant
      Topics: 8
      Replies: 9
      Points: 158
      Rank: Participant

      The org file is about 4000 – 5000 records CSV file. Every week, I have a list of 300-400 new build to verify hardware configuration.

      If I use “where”, it takes forever. If I use select-string, it is much faster.

    • #250112
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,690
      Helping Hand
      Rank: Community Hero

      Welcome to the matrix. In reality, select-string returns a matchinfo object. The default custom view puts filename and linenumber in front of the result “line” joined by colons. This is defined in a .format.ps1xml file somewhere, I think $pshome\PowerShellCore.format.ps1xml. Get-date also returns a custom view by default. Most commands have a default table view.

      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
      • This reply was modified 1 month, 1 week ago by js.
    • #250142
      Participant
      Topics: 8
      Replies: 568
      Points: 2,170
      Helping Hand
      Rank: Community Hero

      This answer seems perfect then.

      (Select-string -Pattern PC101 -path .\abc.csv).Line

      The same premise but written differently

      Select-string -Pattern PC101 -path .\abc.csv | select -ExpandProperty line

      • This reply was modified 1 month, 1 week ago by Doug Maurer.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.