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

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

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

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

This will work…

[pre]

Select-string -Pattern PC101 -path .\abc.csv | Format-Wide Line

or

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

[/pre]

Thank you.

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.

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.

get-childitem file | select-string hi | format-custom

file:1:hi


get-childitem file | select-string hi | format-list

IgnoreCase : True
LineNumber : 1
Line       : hi
Filename   : file
Path       : C:\Users\admin\foo\file
Pattern    : hi
Context    :
Matches    : {0}


get-childitem file | select-string hi | % line

hi

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