Tech Blog‎ > ‎

PowerShell - Print All Odd/Even Lines

posted Mar 30, 2010, 8:59 AM by Victor Zakharov
As a little brainstormer, suppose you have a variable with the following test content:

$str = @"
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
"@

Here is how you can print all odd lines from it:

$f=0; $str.Split("`n") | % { if($f = !$f) { $_ } }

Or, if you want all even lines, change the initial $f value to 1:

$f=1; $str.Split("`n") | % { if($f = !$f) { $_ } }
Comments