0

I'm trying to check if string(s) are present in a file using the below option: (tried in IRB)

File.readlines("E:/nano/ABC.txt").grep(/Digital/).any?
=> true

gives me true, so string is available but if I check for ids like below

File.readlines("E:/nano/ABC.txt").grep(/ncr\abc_efg_dev/).any?
=> false

or

File.readlines("E:/nano/ABC.txt").grep(/ncr\yui30n/).any?
=> false

it's giving false and it's unable to identify the string even though it's present in the file. I'm not sure how to check the existence of the strings - 'ncr\jx8go5' or 'ncr\atxe5t'

ABC.txt

# Digital created on July 2016
# Digital Owner: John Cena (jxcgo)
# Digital access: create delete access

[groups]
first = ncr\abc_efg_dev, ncr\abc_efg_test, ncr\jx8go5, ncr\atxe5t
digital_owner = ncr\yui30n, ncr\bhyrl4
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
Goku
  • 482
  • 1
  • 7
  • 22

1 Answers1

1

You need to escape the back-slash. For example:

File.readlines("E:/nano/ABC.txt").grep(/ncr\\abc_efg_dev/).any?

Or for a fully generic solution, you can use Regexp#escape:

search_string = 'ncr\abc_efg_dev'
File
  .readlines("E:/nano/ABC.txt")
  .grep(Regexp.new(Regexp.escape(search_string)))
  .any?

This is because, for example, \a is a special character called the Bell code.

Other examples include \n (newline), \t (tab), \f (form feed), \v (vertical tab), \r (a carriage return), \s (any white-space character), \b (a backspace or word boundary, depending on context), ...

Long story short, you should always escape \ in a regular expression (or any double-quoted string in ruby!), unless you are intentionally using it to denote a special character.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Thank you, how to escape if it is a variable substitution scenario. ( inside a loop) Eg, I'm reading string values 1 by 1 from a different file and check if that string is present in any of the files inside a folder. values.txt `ncr\yui30n ncr\bhyrl4 ncr\abc_efg_dev` check - ncr\yui30n is present in any of the files in a directory inside a foreach loop, then check ncr\bhyrl4 is present in any of the files. so on.. – Goku May 12 '17 at 10:34
  • 1
    For a fully generic solution, you can use [Regexp#escape](https://ruby-doc.org/core/Regexp.html#method-c-escape): `Regexp.new(Regexp.escape('ncr\yui30n'))`. However, this is admittedly a little clunky... If you are always just searching for a literal string (i.e. you don't really need to use a regex at all), you could also consider [using `String#include?`](http://stackoverflow.com/a/8414129/1954610). – Tom Lord May 12 '17 at 10:40
  • Thanks much. I'm not able to replace the given string in multiple files one by one in a loop. sorry I know its not ethical to ask here but I'm not sure why the target files are getting modified below is my code `value=File.open('E:\nano\out.txt').read value.each_line do |line| line.chomp! Dir.glob("E:\nano\*.txt") do |file_name| text = File.read(file_name) replace = text.gsub(Regexp.escape("#{line}"), "") File.open(file_name, "w") { |file| file.puts replace } end end` – Goku May 12 '17 at 11:08
  • I fixed it , there was some incorrect folder name. thanks for ur time:) – Goku May 12 '17 at 11:20