selected field assignment

B

Bil Kleb

Hi,

I'm grabbing the owner and file names from `find . -perm -g+r -ls`
which has ls -dgils output like,

6744065 8 -rw-r--r-- 1 kleb users 289 Nov 30
2007 Ruby/README

with something like,

find_result.each_line do |line|
fields = line.split
user_name, file_name = fields[4], fields.last

I've also played with,

_,_,_,_,user,_,_,_,_,_,file = a.split

But both leave me thinking there is a better way without resorting
to a regex?

Thanks,
 
T

Tim Pease

Hi,

I'm grabbing the owner and file names from `find . -perm -g+r -ls`
which has ls -dgils output like,

6744065 8 -rw-r--r-- 1 kleb users 289 Nov 30
2007 Ruby/README

with something like,

find_result.each_line do |line|
fields = line.split
user_name, file_name = fields[4], fields.last

I've also played with,

_,_,_,_,user,_,_,_,_,_,file = a.split

But both leave me thinking there is a better way without resorting
to a regex?

class LsInfo <
Struct
new
:)a
, :b
, :permissions
, :type, :user, :group, :size, :month, :day, :year_or_time, :file)

def initialize( string )
super(*string.split)
end
end


info = []
find_result.each_line do |line|
info = LsInfo.new(line)
end

info.first.user
info.last.file


Not being up to snuff on the output format of ls, you'll have to
appropriately name "a" and "b" and whatever else I mislabeled in the
example above.

Blessings,
TwP


PS Thanks for singlehandedly keeping the space shuttle flying with
your 42 line ruby script ;)
 
D

Diogo Lisboa

Hi,

I'm grabbing the owner and file names from `find . -perm -g+r -ls`
which has ls -dgils output like,

6744065 8 -rw-r--r-- 1 kleb users 289 Nov 30
2007 Ruby/README

with something like,

find_result.each_line do |line|
fields = line.split
user_name, file_name = fields[4], fields.last

I've also played with,

_,_,_,_,user,_,_,_,_,_,file = a.split

But both leave me thinking there is a better way without resorting
to a regex?

owner, file = string.split.values_at(4, -1)

It relies on the output being the same format. For other cases a regex
would be better.



Diogo
 
A

ara.t.howard

Hi,

I'm grabbing the owner and file names from `find . -perm -g+r -ls`
which has ls -dgils output like,

6744065 8 -rw-r--r-- 1 kleb users 289 Nov 30
2007 Ruby/README

with something like,

find_result.each_line do |line|
fields = line.split
user_name, file_name = fields[4], fields.last

I've also played with,

_,_,_,_,user,_,_,_,_,_,file = a.split

But both leave me thinking there is a better way without resorting
to a regex?

given that you have an array, and fields, you could use arrayfields ;-)


a = Array.fields %w( size foo mode bar user group )

find_result.each_line do |line|
a.replace line.split

p a['user']
end


a @ http://codeforpeople.com/
 
J

Jeremy Hinegardner

Hi,

I'm grabbing the owner and file names from `find . -perm -g+r -ls`
which has ls -dgils output like,

6744065 8 -rw-r--r-- 1 kleb users 289 Nov 30
2007 Ruby/README

with something like,

find_result.each_line do |line|
fields = line.split
user_name, file_name = fields[4], fields.last

I've also played with,

_,_,_,_,user,_,_,_,_,_,file = a.split

But both leave me thinking there is a better way without resorting
to a regex?

How about this crazy method if you have a find(1) which has -printf.

% ruby eval-find.rb
Executing command: find . -perm -g+r -type f -printf ' "%p" => "%u",\n' >> file_listing.rb
Found 17306 files

% find . -perm -g+r -type f | wc -l
17306

% cat eval-find.rb
#!/usr/bin/env ruby

dir = ARGV.shift || "."

file_listing = "file_listing.rb"

# opening brace for hash
File.open( file_listing, "w+" ) { |f| f.puts "{" }

# use find to dump files to an evalable hash format
format = " \"%p\" => \"%u\",\\n"
cmd = "find #{dir} -perm -g+r -type f -printf '#{format}' >> #{file_listing}"
puts "Executing command: #{cmd}"
%x[ #{cmd} ]

# closing brace for hash
File.open( file_listing, "a" ) { |f| f.puts "}" }

# now eval the hash
listing = eval( IO.read( file_listing) )
puts "Found #{listing.size} files"

enjoy,

-jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top