List to store data generate by split method - freemarker

I am trying to split an input string based on space .
i/p : "darshan kamat"
I need to split above string into 2 different tags in FTL .
expected o/p :
<fname>darshan</fname>
<lname>kamat</lname>
But with split i am just able to iterate print same output which does not help
<#list name?split("\\s+", "r") as x>
${x}
</#list>
With <#assign var= List> throws error as it expects string only
How to store and access list, something like {x[0]} ,{x[1]}
Any pointers please as how to approach?

Related

How to Write Series in FreeMarker?

I am trying to write a Numeric and Alphabetic Series in Free-marker. However I am not able to implement it.
I have tried various portal and Freemarker website itself, but was not able to find a proper solution.
<#assign count = 0>
<#assign seq = ['a','b','c','d','e','f',]>
<#list params_list as test_param>
${count} ${seq[count]}
<#assign count = count + 1>
</#list>
It will print data in
1 a
2 b
3 c
You can use ?lower_abc (or ?upper_abc) to convert a number to a letter, where 1 corresponds to letter "a". If this is inside #list, then you can get the 1-based item counter with itemVariable?counter. For example:
<#list items as item>
${item?counter} ${item?counter?lower_abc}
</#list>

Parse a string in specific format and store its content for further processing

I have a string in the format as below. The string on the LHS could be any string and the result on the RHS has values in the {} of varying length and some are separated by delimiters.
I am not able to understand on how to be able to extract the LHS and RHS into two distinct variables.
Input String format:
[TEAM DETAILS]={2,TeamName,23,4697}
I want to be able to extract the LHS let us say into an array.
For the RHS, I need to process each entry separated by the comma and stored them into an array as well.
I cannot understand on how to do this. It looks simple but I am not able to get a logic out of it.
This script:
# input
in="[TEAM DETAILS]={0001/0880,TeamName,0881,0882/3999,8400/8499,4900/4999,6900/6999,9101,9104,5851,5850,5855,7697}"
# get var name
# remove everything after ]=
var="${in%]=*}"
# remove the leading [
var="${var#[}"
# get values
# remove everything before ={
valstr="${in#*={}"
# remove trailing }
valstr="${valstr%'}'}"
# read string as array
IFS=, read -r -a "values" <<<"$valstr"
# output
declare -p var values
will output in repl:
declare -- var="TEAM DETAILS"
declare -a values=([0]="0001/0880" [1]="TeamName" [2]="0881" [3]="0882/3999" [4]="8400/8499" [5]="4900/4999" [6]="6900/6999" [7]="9101" [8]="9104" [9]="5851" [10]="5850" [11]="5855" [12]="7697")

Extract 2 fields from string with search

I have a file with several lines of data. The fields are not always in the same position/column. I want to search for 2 strings and then show only the field and the data that follows. For example:
{"id":"1111","name":"2222","versionCurrent":"3333","hwVersion":"4444"}
{"id":"5555","name":"6666","hwVersion":"7777"}
I would like to return the following:
"id":"1111","hwVersion":"4444"
"id":"5555","hwVersion":"7777"
I am struggling because the data isn't always in the same position, so I can't chose a column number. I feel I need to search for "id" and "hwVersion" Any help is GREATLY appreciated.
Totally agree with #KamilCuk. More specifically
jq -c '{id: .id, hwVersion: .hwVersion}' <<< '{"id":"1111","name":"2222","versionCurrent":"3333","hwVersion":"4444"}'
Outputs:
{"id":"1111","hwVersion":"4444"}
Not quite the specified output, but valid JSON
More to the point, your input should probably be processed record by record, and my guess is that a two column output with "id" and "hwVersion" would be even easier to parse:
cat << EOF | jq -j '"\(.id)\t\(.hwVersion)\n"'
{"id":"1111","name":"2222","versionCurrent":"3333","hwVersion":"4444"}
{"id":"5555","name":"6666","hwVersion":"7777"}
EOF
Outputs:
1111 4444
5555 7777
Since the data looks like a mapping objects and even corresponding to a JSON format, something like this should do, if you don't mind using Python (which comes with JSON) support:
import json
def get_id_hw(s):
d = json.loads(s)
return '"id":"{}","hwVersion":"{}"'.format(d["id"], d["hwVersion"])
We take a line of input string into s and parse it as JSON into a dictionary d. Then we return a formatted string with double-quoted id and hwVersion strings followed by column and double-quoted value of corresponding key from the previously obtained dict.
We can try this with these test input strings and prints:
# These will be our test inputs.
s1 = '{"id":"1111","name":"2222","versionCurrent":"3333","hwVersion":"4444"}'
s2 = '{"id":"5555","name":"6666","hwVersion":"7777"}'
# we pass and print them here
print(get_id_hw(s1))
print(get_id_hw(s2))
But we can just as well iterate over lines of any input.
If you really wanted to use awk, you could, but it's not the most robust and suitable tool:
awk '{ i = gensub(/.*"id":"([0-9]+)".*/, "\\1", "g")
h = gensub(/.*"id":"([0-9]+)".*/, "\\1", "g")
printf("\"id\":\"%s\",\"hwVersion\":\"%s\"\n"), i, h}' /your/file
Since you mention position is not known and assuming it can be in any order, we use one regex to extract id and the other to get hwVersion, then we print it out in given format. If the values could be something other then decimal digits as in your example, the [0-9]+ but would need to reflect that.
And for the fun if it (this preserves the order) if entries from the file, in sed:
sed -e 's#.*\("\(id\|hwVersion\)":"[0-9]\+"\).*\("\(id\|hwVersion\)":"[0-9]\+"\).*#\1,\3#' file
It looks for two groups of "id" or "hwVersion" followed by :"<DECIMAL_DIGITS>".

Freemarker: Output comma separated list as an array

I have a table that has a field that contains a comma separated list of order IDs. What I am trying to do is output each of those IDs separately so that I can use them to look up their corresponding order details in another table.
Does anyone know of a good way to do this?
So far I have tried:
<#data Alerts_test_table as alerts_test>
<#filter
CUSTOMER_ID_=CONTACTS_LIST.CUSTOMER_ID_1>
<#fields AD_ID_LIST>
<#assign seq = ['${alerts_test.AD_ID_LIST}']>
<#list seq?chunk(1) as row><#list row as cell>
${cell}
</#list> </#list>
</#data>
But this just outputs it as a line of text.
Let's say you have comma separated ID-s in idsString, and you want to iterate though the ID-s one by one. Then:
Data-model (with http://try.freemarker.org/ syntax):
idsString = "11, 22, 33"
Template:
<#list idsString?split(r'\s*,\s*', 'r') as idString>
${idString}
</#list>
However, in the template you have posted I see many strange things, so some ideas/pointers:
Be sure that alerts_test.AD_ID_LIST is indeed a String, not a List that you can list directly, without ?split-ing. If it's a String, then '${alerts_test.AD_ID_LIST}' is ultimately the same as alerts_test.AD_ID_LIST. In general, you never need an empty string literal and an ${} in it. It's not useful (but sometimes harmful, as it converts non-string values to string).
?chunk(1) is not useful. The point of ?chunk is to slice a list to smaller lists, but if those smaller lists are to be 1 long, then you might as well just list the items of the original list.
There are no such directives as #data and #filter. Is this some forked FreeMarker version?
If the ID is a number that you need to pass to some API that expects a number, then you will have to convert it to number like idString?number.

How to use freemarker list with hashmap variable

In Java I am using a
HashMap<String, String>
which is then available in my freemarker template.
I can access it like this
Time:${candidFieldsList["STD-TIME_Environmental_1"]}
This will extract the value for the key STD-TIME_Environmental_1 from my map, this works fine. Now I need to combine this with a list to reduce the redundant code.
I have a area in my template which need to be repeated 4 times
Time:${candidFieldsList["STD-TIME_Environmental_1"]}
The difference to the other parts are only the number, so i tried to use the list to solve this. But it did not work
<#list 1..4 as x>
Time:${candidFieldsList["STD-TIME_Environmental_"${x}]}
</#list>
Instead of returning the value for the key, it returns a parser exception or the string of this expression.
freemarker.core.ParseException: Encountered "$" at line 4, column 50 in template.ftl.
Was expecting one of:
"]" ...
"." ...
"[" ...
"(" ...
"?" ...
"!" ...
Try using the + operator to concatenate the strings:
<#list 1..4 as x>
Time:${candidFieldsList["STD-TIME_Environmental_" + x]}
</#list>

Resources