Spring Batch EL expression - can't escape # sign - spring

I am using Spring Batch 2.1.9 and have a requirement to write a file name with some text and the run date delimited by a # sign. Unfortunately I can't find a way to display a # sign without breaking the expression. I have tried the following formats, all to no avail:
File##{jobParameters[rundate]}
File#{'#' + jobParameters[rundate]}
File${'#'}#{jobParameters[rundate]}
File#{'#'}#{jobParameters[rundate]}
Anytime that extra pound sign is included, the rest of the expression fails to display anything. Is there an obscure way to escape a pound sign?

Have you tried to use String.format("%s#%s",file,rundate) as SPEL?

Related

How to install a maven archetype that has Kotlin code which contains string interpolation?

I'm trying to install a maven archetype that I created from a Kotlin project. Whenever I try to install the archetype, I get this error:
Archetype IT 'basic' failed: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Encountered "()}\"\n
the line of code that triggers the error is
return "redirect:${getRequestMapping()}"
Is there anything that I can do to solve this issue?
I use string interpolation a lot, and I don't want to replace them with concatenated strings
The dollar sign '$' has meaning to Apache Velocity, which is the engine used by the archetypes. Velocity sees the '$', thinks it's supposed to do something with it, but the syntax is wrong (for Velocity) and it fails.
The fix here is to escape the dollar sign so Velocity ignores it as described in documentation.
Something like this, which shows the dollar sign, but also others that might need to be escaped depending on use case:
## File will be filtered by Velocity - it is a Velocity template.
## Establish escape sequences for Velocity special chars.
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
## Use the variable anywhere the interpolation is used
return "redirect:${symbol_dollar}{getRequestMapping()}"
The Velocity docs show this same technique using just 'D' as the variable name. I like the longer name for searchability and self-documentation.

How to obtain basename in ruby from the given file path in unix or windows format?

I need to parse a basename in ruby a from file path which I get as input. Unix format works fine on Linux.
File.basename("/tmp/text.txt")
return "text.txt".
However, when I get input in windows format:
File.basename("C:\Users\john\note.txt")
or
File.basename("C:\\Users\\john\\note.txt")
"C:Usersjohn\note.txt" is the output (note that \n is a new line there), but I didn't get "note.txt".
Is there some nice solution in ruby/rails?
Solution:
"C:\\test\\note.txt".split(/\\|\//).last
=> "note.txt"
"/tmp/test/note.txt".split(/\\|\//).last
=> "note.txt"
If the Linux file name doesn't contain \, it will work.
Try pathname:
require 'pathname'
Pathname.new('C:\Users\john\note.txt').basename
# => #<Pathname:note.txt>
Pathname docs
Ref How to get filename without extension from file path in Ruby
I'm not convinced that you have a problem with your code. I think you have a problem with your test.
Ruby also uses the backslash character for escape sequences in strings, so when you type the String literal "C:\Users\john\note.txt", Ruby sees the first two backslashes as invalid escape sequences, and so ignores the escape character. \n refers to a newline. So, to Ruby, this literal is the same as "C:Usersjohn\note.txt". There aren't any file separators in that sequence, since \n is a newline, not a backslash followed by the letter n, so File.basename just returns it as it receives it.
If you ask for user input in either a graphical user interface (GUI) or command line interface (CLI), the user entering input needn't worry about Ruby String escape sequences; those only matter for String literals directly in the code. Try it! Type gets into IRB or Pry, and type or copy a file path, and press Enter, and see how Ruby displays it as a String literal.
On Windows, Ruby accepts paths given using both "/" (File::SEPARATOR) and "\\" (File::ALT_SEPARATOR), so you don't need to worry about conversion unless you are displaying it to the user.
Backslashes, while how Windows expresses things, are just a giant nuisance. Within a double-quoted string they have special meaning so you either need to do:
File.basename("C:\\Users\\john\\note.txt")
Or use single quotes that avoid the issue:
File.basename('C:\Users\john\note.txt')
Or use regular slashes which aren't impacted:
File.basename("C:/Users/john/note.txt")
Where Ruby does the mapping for you to the platform-specific path separator.

Regex - How can I remove specific characters between strings/delimiters?

This is related to cleaning files before parsing them elsewhere, namely, malformed/ugly CSV. I see plenty of examples for removing/matching all characters between certain strings/characters/delimiters, but I cannot find any for specific strings. Example portion of line would look something like:
","Should now be allowed by rule above "Server - Access" added by Rich"\r
To be clear, this is not the entire line, but the entire line is enclosed in quotes and separated by "," and ends in ^M (Windows newline/carriage return).The 'columns' preceding this would be enclosed at each side by ",". I would probably use this too to remove cruft that appears earlier in the line.
What I am trying to get to is the removal of all double quotes between "," and "\r ("Server - Access" - these ones) without removing the delimiters. Alternatively, I may just find and replace them with \" to delimit them for the Ruby CSV library. So far I have this:
(?<=",").*?(?="\\r)
Which basically matches everything between the delimiters. If I replace .*? with anything, be that a letter, double quotes etc, I get zero matches. What am I doing wrong?
Note: This should be Ruby compatible please.
If I understand you correctly, you can use negative lookahead and lookbehind:
text = '","Should now be allowed by rule above "Server - Access" added by Rich"\r'
puts text.gsub(/(?<!,)"(?![,\\r])/, '\"')
# ","Should now be allowed by rule above \"Server - Access\" added by Rich"\r
Of course, this won't work if the values themselves can contain comas and new lines...

Using the `$` character in `ruby_block` in chef

I want to use the following code in my recipe for ruby_block, but it's not working because of the '$'. The code cannot find $NAME, but it can find NAME. Can you give me a solution?
file.search_file_replace_line("DEFAULT=/etc/default/$NAME","DEFAULT=/etc/default/tomcat7")
search_file_replace_line expects regex as the first argument. And dollar sign is a special symbol within the regular expressions, it means end of the line, basically. So you have to properly escape it if you really want to replace it with something.
This will do the job:
file.search_file_replace_line("DEFAULT=/etc/default/\\$NAME","DEFAULT=/etc/default/tomcat7")

How to escape the '#' symbol in bash?

I am trying to add my proxy with authentication parameters in bash, where my password contains an # symbol. The syntax to add proxy with authentication in bash is as follows:
export http_proxy=http://username:password#host:port_no/
Therefore, whenever I try to add a password with # in it, the applications that use this proxy try to connect to the string followed by the # symbol in the password.
For example, if my password is p#ssword, and the host is proxy.college.com, the applications try to connect to ssword#proxy.college.com.
I have tried escaping the # symbol using \, but this does has not solved.
How do I make this work without changing my password?
Note: This question is not similar to How can i escape an arbitrary string for use as a command line argument in bash nor How to escape the at sign in bash since this specifically treats the '#' sign that comes up in commands where there is an # symbol already present and the # is used to delimit the given string into specific paramters.
P.S.: Though using the HTML code %40 for # works, I would prefer a more readable method.
You can use %40 instead of an # sign.

Resources