Sled
Sled

Reputation: 18939

How to tell if processing the last line of input in a Powershell cmdlet?

I want to know how to test if I am currently processing the last line of input in a commandlet (this might not be the correct term) in Powershell.

I have a series of SQL statments in a log file and I want to construct an union of them. Currently I have (on one line, but I show them separated here for readability here)

echo "SELECT * FROM ( " > union.log
cat sql.log | %{$_ + "`n UNION `n"} >> union.log
echo " ) AS qwerty " >> union.log

However, this results in a superfluous UNION in the output. Therefore, I could like to test in the cmdlet (that's what I think the %{...} is called) if the current line is the last one and then not append the string "UNION".

Is this even possible?


PS- This is a simplified example, in my real case I have to do some grepping and substring to extract the queries.

Upvotes: 0

Views: 63

Answers (1)

Richard
Richard

Reputation: 108975

Better to use the -join operator if you want to take a series of strings and merge them together with something between.

Eg.

@('a', 'b', 'c') -join ':'

results in a:b:c.

Upvotes: 2

Related Questions