How to use Regex Capturing Group variable in NiFi Expression Language? - apache-nifi

I am trying to replace a date format in all lines of text file using NiFi. The file looks like this:
ABCDE,20200619,23.8
FGHIJ,20200619,14.5
...
I am trying to do this using ReplaceText processor to change 20200619 to 2020-06-19. I've made regex expression matching the date ((20\d{6},)) and I have checked that it's working: when i write $1 TESTING, in Replacement value it works as expected (single line of file looks like ABCDE,20200619, TESTING,23.8).
The problem is when I try to use Expression Language and :substring function. This is my code in Replacement value:
${$1:substring(0, 4)}-${$1:substring(4, 6)}-${$1:substring(6, 8)}
But I get following error:
NiFi Error
It looks like the Expression Language can't access my $1 variable. How can I access my Regex Capturing Group variable inside Expression Language?
This is my processor:
NiFi Processor

I found the answer: when trying to access Regex Capturing Group inside ${...} we need to use it with apostrophes, so the code like this works:
${'$1':substring(0, 4)}-${'$1':substring(4, 6)}-${'$1':substring(6, 8)}

Related

How to obtain variable values from a text file for use by a bash script?

I have a bash script, and I'd like it to read in the values of variables from a text file.
I'm thinking that in the text file where the values are stored, I'd use a different line for each variable / value pair and use the equals sign.
VARIABLE1NAME=VARIABLE1VALUE
VARIABLE2NAME=VARIABLE2VALUE
I'd then like my bash script to assign the value VARIABLE1VALUE to the variable VARIABLE1NAME, and the same for VARIABLE2VALUE / VARIABLE2NAME.
Since the syntax of the file is the syntax you would use in the script itself, the source command should do the trick:
source text-file-with-assignments.txt
alternately, you can use . instead of source, but in a case like this, using the full name is more clear.
The documentation can be found in the GNU Bash Reference Manual.

how I could replace in gsub ruby using a replace string with regex command?

I have a command:
ruby -e ' variablea="MTH;VERDER;GHK" ;variableb="MTH;VERDER;GHK"; input=variableb.gsub("#{variablea}".gsub(/.+?;(.*?);.+/,"\\1.+"),"2"); print input ' >fffc
I need to replace MTH;VERDER;GHK using a regex command VERDER.+ generated by substitution "MTH;VERDER;GHK".gsub(/.+?;(.*?);.+/,"\\1.+") for obtain MTH;2.
I understand that the question may be strange but what happens is that I have simplified my approach so as not to make it too complicated to understand.
In summary I'm trying to use a regex command for replace originated from a string will replaced.
My problem is that replace string is treated as string, I cannot use as a regex command.

Parse past all occurrences of a delimiter

I am new to Bash, but hoping this is simple to do. I have the following couple lines of code:
LOCATION='C:\\proj\\myproject\\node_modules\\protractor\\node_modules\\webdriver-manager\\selenium\\chromedriver_2.29.exe'
FILENAME=${LOCATION}
How do I parse past all the backslashes, go to the end of the path, extract the file name and assign it to $FILENAME (in this case 'chromedriver_2.29.exe') ?
This should do the trick:
FILENAME=${LOCATION##*'\\'}
See details on parameter expansion in Bash here.

Can we split a file column by :: delimiter in pig

I am trying to read a file which have delimiter as double colon (::). I am using CSVExcelStorage, but it is giving error as:
could not instantiate 'org.apache.pig.piggybank.storage.CSVExcelStorage' with arguments '[::]'
So is there any way to read a file using custom delimiter?
You can use PigStorage with your custom delimiter.
You are probably missing the quotes.
REGISTER /usr/lib/pig/piggybank.jar;
A = LOAD 'Test.csv' USING org.apache.pig.piggybank.storage.CSVExcelStorage('::')

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")

Resources