3

is there a way to replace multiple strings in a single line of array using gsub.

here is the log file

13:29:00 (alex) OUT: "72500_2010_0F" abcd@S400532  
13:31:12 (alex) IN: "72600_2010_0F" abnc@S403818
13:31:52 (alex) IN: "71200_2010_0F" osa@S400583

I would like to replace the below

(alex) with "" 
OUT:  with OUT
IN: with IN
"72500_2010_0F" with XYZ
"71200_2010_0F" with QWE
"72600_2010_0F" with UIO

The original file (log file) is put into array and I wanted to read each line in the array and perform gsub. Dont want to use Hash as its lil complicated -As being Ruby Beginner...& to Regex...

array1.each do |element|
  i = element.gsub(/?????????/) [ What should go here]
  array2.push(i)
end

I dint want to make changes to original array but push the modified lines into different array i.e array2

What is the best & easy to understand code to do this? - Please help

2
  • Why not use file operations to read the file, read every line, replace the content of line with whatever you want and write the new line to a new file ? Commented May 8, 2014 at 7:30
  • How do we do that? - I'm loading file into an array and reading each line array and changing its contents one by one... may be not so good approach yeah? Commented May 9, 2014 at 0:29

1 Answer 1

7

Looking at String#sub it seems there are many ways to do what you want:

  1. One approach can be to use a hash as second parameter

    def cleansed_log_line(line)
      replacement_rules = {
        'alex' => '',
        'OUT: ' => 'OUT ',
        'IN: ' => 'IN '
      }
      matcher = /#{replacement_rules.keys.join('|')}/
    
      line.gsub(matcher, replacement_rules)
    end
    
  2. Another approach can be to to use block form

    def cleansed_log_line(line)
      replacement_rules = {
        'alex' => '',
        'OUT: ' => 'OUT ',
        'IN: ' => 'IN '
      }
      matcher = /#{replacement_rules.keys.join('|')}/
    
      line.gsub(matcher) do |match|
        replacement_rules[match] || match
      end
    end
    
  3. Another not so good implementation

    def cleansed_log_line(line)
      replacement_rules = {
        /alex/ => '',
        'OUT: ' => 'OUT ',
        'IN: ' => 'IN '
      }
    
      replacement_rules.each do |match, replacement|
        line = line.gsub(match, replacement)
      end
    
      line
    end
    

Hashes and RegEx are nothing to worry about. This function can cleanse any line passed to it and would return a cleansed line.

Sign up to request clarification or add additional context in comments.

1 Comment

hi, like your 1st approach - and is easy to understand. I'm guessing, line.gsub(matcher, replace...) will look into each key in a line and replace this with 'key value' is that right? Thanks for your hlep mate...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.