2011-08-05 31 views
9

खोल में अंदर उत्पादन पर कार्यक्षमता की तरह ग्रेप प्राप्त करने के लिए एक रास्ता है, मैं अंदर रेल कंसोलवहाँ रेल कंसोल

$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for" 

कर सकते हैं, मैं ऐसा ही कुछ हासिल कर सकते हैं, का कहना है कि जब मैं एक कमांड चलाने के लिए और आउटपुट बहुत बड़ा है, खासकर डीबी क्वेरी कहें।

मुझे इसे YAML के रूप में आउटपुट करने के बारे में पता है, लेकिन यह वह नहीं है जिसे मैं ढूंढ रहा हूं।

धन्यवाद।

उत्तर

19

हाँ, आप कर सकते हैं। विधि जीआर कहा जाता है ... इसके लिए प्रतीक्षा करें ... एपी। रूबी का grepString, Array और कई अन्य अंतर्निर्मित वस्तुओं पर काम करता है। उदाहरण के एक नंबर के सभी to_xxx तरीकों पाने के लिए के लिए, बस कार्य करें:

1.methods.grep(/to_/) 
+1

डीबी क्वेरी से उत्पादन पर काम नहीं करता। – subiet

+1

मैं बस हंसी में फूट गया ... "जीआर .. इसके लिए प्रतीक्षा करें .." ... एक अच्छा हास्य की तरह;) – luigi7up

2

मैं एक ही सवाल था और नहीं ream88 से कुछ हद तक snarky प्रतिक्रिया से बहुत संतुष्ट था, इसलिए मैंने इसे एक दरार लेने का फैसला किया।

# Allows you to filter output to the console using grep 
# Ex: 
# def foo 
#  puts "Some debugging output here" 
#  puts "The value of x is y" 
#  puts "The value of foo is bar" 
# end 
# 
# grep_stdout(/value/) { foo } 
# # => The value of x is y 
# # => The value of foo is bar 
# # => nil 
def grep_stdout(expression) 
    # First we need to create a ruby "pipe" which is two sets of IO subclasses 
    # the first is read only (which represents a fake $stdin) and the second is 
    # write only (which represents a fake $stdout). 
    placeholder_in, placeholder_out = IO.pipe 

    # This child process handles the grep'ing. Its done in a child process so that 
    # it can operate in parallel with the main process. 
    pid = fork { 
    # sync $stdout so we can report any matches asap 
    $stdout.sync 

    # replace $stdout with placeholder_out 
    $stdin.reopen(placeholder_in) 

    # we have to close both placeholder_out and placeholder_in because all instances 
    # of an IO stream must be closed in order for it to ever reach EOF. There's two 
    # in this method; one in the child process and one in the main process. 
    placeholder_in.close 
    placeholder_out.close 

    # loop continuously until we reach EOF (which happens when all 
    # instances of placeholder_out have closed) 
    read_buffer = '' 
    loop do 
     begin 
     read_buffer << $stdin.readpartial(4096) 
     if line_match = read_buffer.match(/(.*\n)(.*)/) 
      print line_match[1].grep(expression) # grep complete lines 
      read_buffer = line_match[2]   # save remaining partial line for the next iteration 
     end 
     rescue EOFError 
     print read_buffer.grep(expression) # grep any remaining partial line at EOF 
     break 
     end 
    end 
    } 

    # Save the original stdout out to a variable so we can use it again after this 
    # method is done 
    original_stdout = $stdout 

    # Redirect stdout to our pipe 
    $stdout = placeholder_out 

    # sync $stdout so that we can start operating on it as soon as possible 
    $stdout.sync 

    # allow the block to execute and save its return value 
    return_value = yield 

    # Set stdout back to the original so output will flow again 
    $stdout = original_stdout 

    # close the main instances of placeholder_in and placeholder_out 
    placeholder_in.close 
    placeholder_out.close 

    # Wait for the child processes to finish 
    Process.wait pid 

    # Because the connection to the database has a tendency to go away when calling this, reconnect here 
    # if we're using ActiveRecord 
    if defined?(ActiveRecord) 
    suppress_stdout { ActiveRecord::Base.verify_active_connections! } 
    end 

    # return the value of the block 
    return_value 
end 

मेरे समाधान की स्पष्ट कमी यह है कि आउटपुट खो गया है। मुझे यकीन नहीं है कि yield को दो बार कॉल किए बिना कैसे पहुंचे।

संपादित करें मैंने अपना जवाब केवल fork पर कॉल करने के लिए बदल दिया है, जो मुझे ब्लॉक के आउटपुट को रखने और अंत में इसे वापस करने की अनुमति देता है। जीत।

संपादित 2 आप इस मणि में इस कार्यक्षमता के सभी प्राप्त कर सकते हैं (और अधिक!) अब https://github.com/FutureAdvisor/console_util

संबंधित मुद्दे