Dawid Gosławski
Dawid Gosławski

Reputation: 2088

Powershell `echo -e "1\n2\n3" | tail -n1` equivalent

There are countless examples about how to get tail for file content, but there is no for pipes.

Is there any way to get last line from pipe ?

I have long chain of piped commands and I want last 1 line only.

Upvotes: 1

Views: 440

Answers (2)

js2010
js2010

Reputation: 27516

Or -1 is the last element of an array.

(echo 1 2 3)[-1]

3

Upvotes: 0

Manuel Batsching
Manuel Batsching

Reputation: 3606

If you want the last object, that is send through the pipe, Select-Object can do that for you:

$input | Select-Object -Last 1

If you are only interested in one particular property of this object, you can expand this property in the same statement:

[pscustomobject]@{propname = 'foo'},[pscustomobject]@{propname = 'bar'} | 
    Select-Object -Last 1 -ExpandProperty propname

Upvotes: 2

Related Questions