Home Escaping regex in a Ruby awk system command
Reply: 1

Escaping regex in a Ruby awk system command

SamuelLJohnson
1#
SamuelLJohnson Published in 2018-01-13 00:07:10Z

The following works directly in my Mac OS X terminal, creating a file with a few lines:

awk '!/^1499\||^1598\||^1599\||^1999\||^2298\||^2299\||^2403\|/' "#{working_path}" > "#{filtered_file_path}"

However, when I attempt to use it in Ruby on Rails using backticks, the resulting file is empty:

`awk '!/^1499\||^1598\||^1599\||^1999\||^2298\||^2299\||^2403\|/' "#{working_path}" > "#{filtered_file_path}"`

An awk with a simple regex works. For example:

`awk '!/SMITH/' "#{working_path}" > "#{filtered_file_path}"`

So, the issue appears to be with the escaped pipe characters.

Ideas?


Some background I should have provided:

The file I am processing is pipe-delimited. I am filtering out lines with certain codes that are in the first value on the line. So, the regex I am using is something like ^2298\|.

The other pipes in the expression in single quotes are regex OR operators.

"working_path" and "filtered_file_path" are Ruby variables.

SamuelLJohnson
2#
SamuelLJohnson Reply to 2018-01-14 05:30:46Z

I just figured it out. The backslash that is escaping the pipe characters also needs to be escaped. Not sure why there is a difference between the regular Terminal and Ruby, but there it is. The working version:

`awk '!/^1499\\||^1598\\||^1599\\||^1999\\||^2298\\||^2299\\||^2403\\|/' "#{working_path}" > "#{filtered_file_path}"`

After challenging my assumption that the problem was Ruby on Rails, the accepted answer here is what explained it: Pipe symbol | in AWK field delimiter

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331439 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO