I need to check if the last number of character of a ${string} is equal to 9.
The string or numbers that I have to handle with is something 831, 519 or 1351.
However I dont know how do do it properly. I tried already something like:
${string?replace((string.length)-1,"9")}
At the end there should be instead of 831 --> 839 or 1351 --> 1359 and so on.
Any sugestions about how I can archive this ?
Oh and by the way. If I use the fuction above this error massage comes up:
Script error: You have used ?number on a string-value which is not a number (or is empty or contains spaces).
And what I tried also was:
code snippet
Because the original number is somethink like 831.896.
You could use string slicing to keep all characters except for the last one like this:
<#assign string = "1234">
<#assign string = string[0..<string?length-1] + "9">
${string}
Results in:
1239
Since you want to replace that thing, use ?replace. This replaces the last character with 9, if the last character is a digit that's not already 9:
${s?replace("[0-8]$", '9', 'r')}
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?
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>".
I want to find & remove a string from string. See the examples below,
Input1:
a = 'mangalore'
Input2
b = 'mc'
Outputs
b values should not present in a for output1 #angalore
a values should not present in b for output2 #c
Solutions: converting string a,b to array then doing a-b & b-a
it will give results. How to implement using string in ruby.
Helps appreciated!
Use String#tr:
main > 'mangalore'.tr 'mc', '#'
#⇒ "#angalore"
main > 'mc'.tr 'mangalore', '#'
#⇒ "#c"
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>