簡體   English   中英

Ruby在迭代期間的Grep命令

[英]Grep command in ruby during iteration

可以在shell腳本中完成,但是自學習以來想嘗試在ruby中嘗試,請對此提供幫助。

嘗試在迭代過程中從文件( list )grep變量( line )時,未顯示任何輸出

但是當像下面單獨做時,可以看到輸出

Getting Output: ("Phone" is one of the lines present in the file)

    value = `grep -i "phone" list`
        puts value

 No Output:
     File.open("list") do |f|
            f.each_line do |line|
            puts line
            value = `grep -i "line" list`
    #       puts list
            puts value
            end
    end

Ruby的寫法是這樣的:

File.open('list').each_line.grep(/phone/i)

文檔:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM