Smarty13
Smarty13

Reputation: 7

Delete line in powershell file

I have a file with the following lines

Servicetag 
P4M8K82 
P4M8l582  
O5M8K82  

I wish I had the serial numbers only and not the name ServiceTag. I would like: Browse the .csv file if the line contains SerivceTag then you don't get the value and copy the rest.

As a result, I wish I had:

P4M8K82 
P4M8l582  
O5M8K82 

Do you think it's possible?

Upvotes: 0

Views: 140

Answers (3)

Walter Mitty
Walter Mitty

Reputation: 18940

Your input file looks like a CSV File with only one field. When there is only one field, there aren't any separating commas. Servicetag is not a data entry. It's the name of the field, also known as a header.

Try the following:

Import-Csv Test.csv | Format-Table

The result is a table with just one column header. If you do something like:

$testdata = Import-Csv Test.csv

now your data is imported in a form suitable for processing.

Upvotes: 0

Theo
Theo

Reputation: 61068

Although not really clear what you are asking, the below line will read the file as string array and write out a new file with all lines containing 'ServiceTag' removed:

Get-Content -Path 'TheOriginalFile'| Where-Object { $_ -notmatch 'ServiceTag' } | Set-Content -Path 'TheNewFile'

Upvotes: 0

Paweł Dyl
Paweł Dyl

Reputation: 9143

Since it is a CSV header, you can just skip first line:

#demo file
@"
Servicetag 
P4M8K82 
P4M8l582  
O5M8K82  
"@ | out-file Test.csv

#solution to get items
cat test.csv | select -Skip 1

#or write them to file
cat test.csv | select -Skip 1 | Out-file output.txt

Upvotes: 1

Related Questions