Skip to content

Instantly share code, notes, and snippets.

@onsails
Created May 10, 2010 07:41
Show Gist options
  • Save onsails/395785 to your computer and use it in GitHub Desktop.
Save onsails/395785 to your computer and use it in GitHub Desktop.
File.open("/etc/passwd").each_line do |line|
arr = line.split(":")
puts "#{arr.first} - #{arr.last}"
end
File.open("/etc/passwd") do |io|
io.each_line do |line|
arr = line.split(":")
puts "#{arr.first} - #{arr.last}"
end
end
# А вот пример через регулярки
File.open("/etc/passwd").each_line do |line|
puts "#{$1} - #{$2}" if line =~ /^(.*?)\:.+\:(.+)$/
end
# И ещё один
File.read("/etc/passwd").scan(/^(.*?)\:.+\:(.+)$/) do |login, shell|
puts "#{login} - #{shell}"
end
# А можно и вовсе внаглую заменить всё gsub'ом:
puts File.read("/etc/passwd").gsub(/^(.*?)\:.+\:(.+)$/, "\\1 - \\2")
# ... с одинарными кавычками
puts File.read("/etc/passwd").gsub(/^(.*?)\:.+\:(.+)$/, '\1 - \2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment