Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a line number on Powershell?

Tags:

powershell

So I have been searching a lot and couldn't find anything that wouldn't return me nothing.

I have a code with a variable and I have a file with a lot of lines on it. For example, I have the following file (things.txt):

Ketchup

Mustard

Pumpkin

Mustard

Ketchup

And what I want to take out is the line numbers of "Mustard". Here's the code I'm trying right now

$search="Mustard"
$linenumber=Get-Content things.txt | select-string $search -context 0,1
$linenumber.context

But it actually returns "". Everyone online was about using context but I only want to know the line number of every "Mustard" which are 2 and 4.

Thanks for your help!

like image 842
Spina97 Avatar asked Apr 26 '18 13:04

Spina97


1 Answers

Select-String returns the line number for you. You're just looking at the wrong property. Change your code to:

$search="Mustard"
$linenumber= Get-Content thing.txt | select-string $search
$linenumber.LineNumber
like image 161
boxdog Avatar answered Sep 22 '22 03:09

boxdog