简体   繁体   English

ruby在行首匹配字符串或空格/制表符,并将uniq行插入文件

[英]ruby match string or space/tab at the beginning of a line and insert uniq lines to a file

This is my code: 这是我的代码:

    File.open(file_name) do |file|
      file.each_line do |line|;
        if line =~ (/SAPK/) || (line =~ /^\t/ and tabs == true) || (line =~ /^ / and spaces == true)
        file = File.open("./1.log", "a"); puts "found a line #{line}"; file.write("#{line}".lstrip!)
        end
      end
    end

File.open("./2.log", "a") { |file| file.puts File.readlines("./1.log").uniq }
  1. I want to to insert all the lines that match a specific string, start with tab or start with space to a file 1.log , all lines should be with space/tab at the beginning so I removed them. 我想将与特定字符串匹配的所有行插入到文件1.log ,以tab开头或以空格1.log ,所有行的开头应以空格/ tab开头,因此我删除了它们。

  2. I want get the unique lines in 1.log and write them to 2.log 我想在1.log获得唯一的行并将其写入2.log

  3. It will be great if some can go over the code and tell me if something is not correct. 如果有人可以检查代码并告诉我一些不正确的信息,那就太好了。

  4. When using files in Ruby, what is the difference between the w+ and a modes? 在Ruby中使用文件时, w+a模式之间有什么区别?

I know: 我知道:

w+ - Create an empty file for both reading and writing. w +-创建一个用于读取和写入的空文件。

a - Append to a file.The file is created if it does not exist. a-附加到文件。如果文件不存在,则创建该文件。

But both options append to the file, I though w+ should behave like > , instead of >> ,so I guess w+ also like >> ? 但是两个选项都附加到文件中,尽管w+应该表现为> ,而不是>> ,所以我猜w+也应该像>>一样?

Thanks !! 谢谢 !!

There's a lot of confusion in this code and it isn't helped by your habit of jamming things on a single line for no reason. 这段代码有很多混乱,而且您习惯于无缘无故地将内容卡在一行上的习惯也无济于事。 Do try and keep your code clean, as the functionality should be obvious. 请尝试保持代码干净,因为功能应该很明显。 There's also a lot of quirky anti-patterns like stringifying strings and testing booleans vs booleans which you should really avoid doing. 还有很多古怪的反模式,例如字符串字符串化和测试booleans vs booleans,您应该避免这样做。

One thing you'll want to do is employ Tempfile for those situations where you need an intermediate file. 您需要做的一件事是在需要中间文件的情况下使用Tempfile

Here's a reworked version that's cleaned up: 这是已清理的重做版本:

Tempfile.open do |temp|
  File.open(file_name) do |input|
    input.each_line do |line|
      if line.match(/SAPK/) || (line.match(/^\t/) and tabs) || (line.match(/^ /) and spaces)
        puts "found a line #{line}"

        temp.write(line.lstrip!)
      end
    end
  end

  File.open("./2.log", "w+") do |file|
    # Rewind the temporary file to read data back
    temp.rewind

    file.write(temp.readlines.uniq)
  end
end

Now a and w+ are largely similar, it's just two ways that are offered for people familiar with whatever notation. 现在aw+在很大程度上相似,这只是为熟悉任何表示法的人提供的两种方法。 It's like how Array has both length and size which do the same thing. 就像Array具有lengthsize的方式一样。 Pick one and use it consistently or your code will be confusing. 选择一个并持续使用它,否则您的代码会造成混乱。

My criticism over things like x == true is because something that narrowly specific usually means that x could take on a multitude of values and true is one particular case we're trying to handle, something that implies that we should be aware it might be false and many other things. 我对x == true类的批评是因为狭义的东西通常意味着x可以采用多种值,而true是我们要处理的一种特殊情况,这意味着我们应该意识到这可能是false和许多其他事情。 It's a red herring and will only invite questions. 这是一条红鲱鱼 ,只会邀请您提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM