Another Hey Scripting Guy ! Question I could not refuse ;-)
How Can I Tell Which Numbers Are Missing From a Sequence of Numbers Found in a Text File? In Powershell ?
let’s make a text file containing the following sequence of numbers:
# Make a TextFile with the Numbers
@'
2
4
8
9
10
'@ | sc test.txt
How are we going to determine which numbers (1, 3, 5, 6, and 7) are missing from the sequence? Like this:
gc test.txt |% {$i = 1}{while ($i -lt $_){$i;$i++};$i++}
PoSH>gc test.txt |% {$i = 1}{while ($i -lt $_){$i;$i++};$i++}
1
3
5
6
7
What’s that? Explain how this script works? Come on, SG; didn’t we mention that it was Saturday?
*Edit* did I hear someone say Sort ? (thx Karl)
*Edit2* Added from file with cast to INTEGER
gc test.txt | sort {[int]$_} |% {$i = 1}{while ($i -lt $_){$i;$i++};$i++}
PoSH>2,9,4,8,10 | sort |% {$i = 1}{while ($i -lt $_){$i;$i++};$i++}
1
3
5
6
7
*Edit3*, Ok, still no explanation but this might help understanding how this script works : PowerShell : Can you do that less cryptic ?
Enjoy,
Greetings, /\/\o\/\/
Tags : Monad msh PowerShell
October 2005 November 2005 December 2005 January 2006 February 2006 March 2006 April 2006 May 2006 June 2006 July 2006 August 2006 September 2006 October 2006 November 2006 December 2006