添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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!

Select-String $search things.txt | Select-Object -ExpandProperty LineNumber – Bill_Stewart Apr 26 '18 at 14:08 @Bill_Stewart that returns me nothing too... Also "Get-Content things.txt | Select-String $search | Select-Object -ExpandProperty LineNumber " more of the same – Spina97 Apr 26 '18 at 14:10

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
                Returns me nothing... I'm starting to think there is something wrong with the program itself...
                    – Spina97
                Apr 26 '18 at 14:13
                Works fine for me.  Is there more going on in your real script than your example?  Does running just Get-Content thing.txt | select-string "Mustard" produce any output?
                    – boxdog
                Apr 26 '18 at 14:17
                Anything from this?  (Get-Content thing.txt | select-string "Mustard").LineNumber
                    – boxdog
                Apr 26 '18 at 14:21
                absolutely nothing... I'm about just to make another file, I can't believe it doesn't work whatever I do
                    – Spina97
                Apr 26 '18 at 14:23
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.27.35032