Take first 5 letter of word and add 3 numbers

April 1, 2011 at 11:28:08
Specs: Windows 7
Using a .txt file with words inside. Taking the first five letters from the words in the txt file and add 001 for each unique word. If the same word exist then add 002, 003, 004 so forth. How would I do this in command prompt? Is it possible in windows?

Input:
Windows
Windows95
Apple
Linux
Computer

Output:
Windo001
Windo002
Apple001
Linux001
Compu001


See More: Take first 5 letter of word and add 3 numbers

Report •

#1
April 1, 2011 at 21:40:32
Sure you can, with a programming language such as Ruby for Windows

h=Hash.new(0)
File.open("file").each do |line|
  line.chomp!
  h[ line[0,5] ] += 1
  puts line[0,5]+ sprintf("%03d" , h[ line[0,5] ] )
end

test run

C:\work>ruby fiveletters.rb
Windo001
Windo002
Apple001
Linux001
Compu001


Report •
Related Solutions


Ask Question