How to Escape the Vertical Bar When Doing Command Line with Ruby

How can I escape the vertical bar when doing command line with Ruby?

I found this microsoft.com page that says the following about pipes and special characters:

The ampersand (&), pipe (|), and parentheses ( ) are special
characters that must be preceded by the escape character (^) or
quotation marks when you pass them as arguments.

How to let a line end with | (pipe) in HAML?

No need for escaping, just the same indentation than the element u want separated.

%div.menu
-if condition1?
#{link_to 'Section 1', section_1_path}
|
-if condition2?
#{link_to 'Section 2', section_2_path}
|
-if condition3?
...

you can try this in browser:
online haml editor: rendera
or
html2haml

Ruby Project - Prevent a ruby file from directly being called from OS command line

You can't do this with file permissions, since the user needs to read the files; removing the read permission means you can't include it either. Removing the execute permission is useful to signal that these file aren't intended to be executed, but won't prevent people from typing ruby incl.rb.


The easiest way is probably to set a global variable in the init.rb script:

#!/usr/bin/env ruby

FROM_INIT = true
require './incl.rb'

puts 'This is init!'

And then check if this variable is defined in the included incl.rb file:

unless defined? FROM_INIT
puts 'Must be called from init.rb'
exit 0
end

puts 'This is incl!'

A second method might be checking the value of $PROGRAM_NAME in incl.rb; this stores the current program name (like argv[0] in many other languages):

unless $PROGRAM_NAME.end_with? 'init.rb'
puts 'Must be called from init.rb'
exit 0
end

I don't recommend this though, as it's not very future-proof; what if you want to rename init.rb or make a second script?

how to extract value from line found

Your regular expression's * is greedy, and will consume all characters it can without stopping the rest of the expression from matching. There is nothing in the expression that tells ruby when to stop collecting characters.

Look at regular-expressions.info. A partial fix for your problem would be to put a '|' after your capture:

tag198=line.match(/198=(.*)\|/)[1] puts tag198

The '|' is escaped as it has special meaning in regexes otherwise. This doesn't yet work though, because the * can still consume '|' characters, so long as it leaves one behind to match the '|' in our expression. To fix completely, prevent the * from capturing any pipes:

tag198 = line.match(/198=([^|]*)\|/)[1] puts tag198

See results of this change here.

How to escape indicator characters (colon and hyphen) in YAML

Quotes:

"url: http://www.some-site.example/"

To clarify, I meant “quote the value” and originally thought the entire thing was the value. If http://www.some-site.example/ is the value, just quote it like so:

url: "http://www.some-site.example/"

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

How can I run a command with pipe character in its parameters via powershell?

Using an example with find you will get similar results.

find /c "this|that|andtheotherthing" C:\temp\EventCombMT.txt

find : FIND: Parameter format not correct
At line:1 char:1
+ find /c "this|that|andtheotherthing" C:\temp\EventCombMT.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (FIND: Parameter format not correct:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Pretty sure this is because the quotes are consumed by the PowerShell interpreter and that leave your mocha command with an unquoted string. Doubling up the quotes is another was to prevent this.

find /c '"this|that|andtheotherthing"' C:\temp\EventCombMT.txt 

It seems this is not the case with mocha? In comments it was determined that we need to reverse the quote set from that seen in the find examples.

There are better ways to call external commands as you have seen in your linked post. Like using the call operator and a hashtable. You would still have to address the quotes though. You could escape a set of double quotes as well to get a similar effect.

find /c "`"this|that|andtheotherthing`"" C:\temp\EventCombMT.txt

For this to be correct though it does not really match the error you are getting. While I am correct about a solution I might be wrong about my interpretation of the issue. PowerShell should not care about the pipe character regardless of how it is quoted. That is what the quotes are for.

Pipe symbol | in AWK field delimiter

The pipe is a special character in a regex, so you need to escape it with a backslash. But this backslash is also a special character for the string literal, so it needs to be escaped again. So you end up with the following:

awk -F '<\\|>' '{$1=$1}1'

awk 'BEGIN {FS="<\\|>"} {$1=$1}1'

The reason for this syntax is explained quite well here: http://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps. In short, the expression is parsed twice.



Related Topics



Leave a reply



Submit