jstl Remove all whitespaces from a string - jstl

I am trying to remove all whitespaces from a jstl variable.
<c:set var="string1" value="This is first String "/>
Trim only removes the trailing spaces but I want the output like:
"ThisisfirstString "
with all spaces removed.Could anyone suggest?

Okay I got the answer:
<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:replace(string1,' ', '')}" />

Related

Loop pcregrep and get one match per index

i try to put all pcregrep matches in a loop but when i have two matches in one line or only the second match. I would like to loop it with one match per loop-index. this is what i have now but i just get the second match:
pcregrep -o1 -r --exclude-dir="_copy" "^.*LLL:EXT:(.*)['\"].*$" Resources/Private/Templates/ContentElements
Example Filecontent:
="panels"
label="LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.sheets.panels">
<flux:field.select name="color"
label="LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color"
items="{ivs:bootstrap.colors()}"
/>
<flux:field.select name="color"
label="LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color"
items="{
0:{
0:'LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:bootstrap.color.primary.key', 1:'LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:bootstrap.color.primary.value'
}
}"
/>
<flux:grid>
<flux:grid.row>
<flux:grid.column colPos="0"
name="content"
label="LLL:EXT:xxx/Resources/Private/Language/locallang.xlf:flux.content"/>
</flux:grid.row>
</flux:grid>
</flux:form.sheet>
Result:
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.sheets.panels
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color
xxx/Resources/Private/Language/locallang.xlf:bootstrap.color.primary.value
xxx/Resources/Private/Language/locallang.xlf:flux.content
Expected Result:
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.sheets.panels
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color
xxx/Resources/Private/Language/locallang.xlf:flux.m-alert.color
xxx/Resources/Private/Language/locallang.xlf:bootstrap.color.primary.key
xxx/Resources/Private/Language/locallang.xlf:bootstrap.color.primary.value
xxx/Resources/Private/Language/locallang.xlf:flux.content

How to use sed to extract the specific substring?

div class="panel-body" id="current-conditions-body">
<!-- Graphic and temperatures -->
<div id="current_conditions-summary" class="pull-left" >
<img src="newimages/large/sct.png" alt="" class="pull-left" />
<p class="myforecast-current">Partly Cloudy</p>
<p class="myforecast-current-lrg">64°F</p>
<p class="myforecast-current-sm">18°C</p>
I try to extract the "64" in line 6, I was thinking to use awk '/<p class="myforecast-current-lrg">/{print}', but this only gave me the full line. Then I think I need to use sed, but i don't know how to use sed.
Assumptions:
input is nicely formatted as per the sample provided by OP so we can use some 'simple' pattern matching
Modifying OP's current awk code:
# use split() function to break line using dual delimiters ">" and "&"; print 2nd array entry
awk '/<p class="myforecast-current-lrg">/{ n=split($0,arr,"[>&]");print arr[2]}'
# define dual input field delimiter as ">" and "&"; print 2nd field in line that matches search string
awk -F'[>&]' ' /<p class="myforecast-current-lrg">/{print $2}'
Both of these generate:
64
One sed idea:
sed -En 's/.*<p class="myforecast-current-lrg">([^&]+)&deg.*/\1/p'
This generates:
64

unix shell replace lowercase to uppercase in spcific lines (xsd tags)

I have file, with tags.
I need to change the strings in "" from lowercase to uppercase, only where the line begins with "<element name"
for example:
<element name="product-info">
<customer name="myname">
the needed output is:
<element name="PRODUCT-INFO">
<customer name="myname">
with sed, you can do the following :
sed -ri 's/element name="(.*)"/element name="\U\1"/' your_file.xml.
It should not mess up the xml structure but be careful.

Is there anyway of converting uppercase to lowercase as well as modifying spaces to text in a file using SED

I have some html text that I need to fix the URL text on. I need to:
1) convert text within the URL to lowercase
also
2) converting any spaces to hyphens within the URL
also
3) deleting any parenthesis from URL
I have multiple occurances of this pattern within each file:
<div class="classname"><img src="${asset.image/url}" alt="TEXT" class="another-class-name" ></div>
Example:
I want to change this pattern: <div class="classname"><img src="${asset.image/url}" alt="TEXT" class="another-class-name" ></div>
To: <div class="classname"><img src="${asset.image/url}" alt="TEXT" class="another-class-name" ></div>
I have a number of files, and want to do an infile substitution. The /URL-EXAMPLE-ONE could have any combination of SPACE, Parenthesis too.
I now have, using the suggestion below:
/sw/bin/sed -e '/<div class="mk-man-logo-mod5-m"><a href="\/[A-Z -{}&]*"></ {
h;
s/.*<div class="mk-man-logo-mod5-m"><a href="\/\(.*\)"><img.*/\1/;
s/\(.*\)/\L\1/;
s/[ &]/-/g;
s/[()]//g;
s/<img.*//;
x;
s/\(.*<div class="mk-man-logo-mod5-m"><a href="\/\)\(.*\)\(<img.*\)/\1\3/;
G;
s/\n//;
}' $e
But the output I'm getting is, as an example:
Original text:
<div class="classname"><img src="${asset.images/common/manufacturer_logos/medium/abb-m.gif}" alt="TEXT" class="another-classname" ></div>
Transformed text:
<div class="classname"><a href="/<img src="${asset.images/url}" alt="TEXT" class="another-classname" abc-d-ediv>
Actually want:
<div class="classname"><img src="${asset.images/url}" alt="TEXT" class="another-classname"></div>
Could anyone help further?
Many thanks in advance,
Alex
A sed script to perform all of the substitutions:
sed '/<div class="classname"><a href="\/.*">/ { h; s/<div class="classname"><a href="\///; s/\(.*\)/\L\1/; s/ /-/g; s/[()]//g; x; s/\(<div class="classname"><a href="\/\)\(.*\)/\1/; G; s/\n//}'
Explanation:
/<div class="classname"><a href="\/.*">/ perform commands only if pattern matched.
h store copy of line in hold space.
s/<div class="classname"><a href="\/// remove all but the url.
s/\(.*\)/\L\1/ convert url to lower case.
s/ /-/g convert spaces to hyphens.
s[()]//g remove parentheses.
x swap hold space with pattern space (now operating on original line again)
s/\(<div class="classname"><a href="\/\)\(.*)/\1/ remove url.
G append hold space to pattern space
s/\n// remove newline introduced by G command.
Alternatively
If your data are always exactly as given in the example (ie. <div class="classname"><a href="/URL">) and you don't need a universal solution, you could use the following simpler script:
sed '/<div class="classname"><a href="\/.*">/ { s/\(.*\)/\L\1/; s/ /-/3g; s/[()]//g }'
This will match the pattern, convert everything to lowercase, delete all parentheses, and replace spaces with hyphens from the third space to the end of the line.
Update
In response to the updated question, here is a new sed command that takes into account the additional <img> tag on the end.
sed '/<div class="classname"><a href="\/[A-Z -(){}&]*"><img.*<\/div>/ { # match pattern
h; # hold copy of original string
# replace original string with just url, converted to lowercase.
s/\(<div class="classname"><a href="\/\)\([A-Z -(){}&]*\)\("><img.*<\/div>\)/\L\2/;
s/[ &]/-/g; # convert spaces, ampersands to hypens.
s/[(){}]//g; # remove parentheses, braces.
G; # append original string after url. Looks like: "converted-url\n<div class="classname"..."
s/\n//; # remove newline introduced by append G.
# swap out URL in original string with converted url.
s/\([a-z-]*\)\(<div class="classname"><a href="\/\)\([A-Z -(){}&]*\)\("><img.*<\/div>\)/\2\1\4/;
}'
Love it or hate it, sometimes Perl is the easiest.
perl -pe 's/<div class="classname"><a href="([^"]*)"/$1 =~ tr:A-Z ():a-z-:rd/e'
The regular expression could be adjusted if it is too general; I didn't do it because the regex in the OP seems to exclude hrefs with parentheses, but the request asks for parentheses to be deleted.

Sed backreference not working properly

Test Data:
<img src=\"images/docs/mydash_grooms.png\" alt=\"\" />
Sed:
sed 's/<img\ssrc=\\"images\/docs\/\([[:graph:]]\)/<a class=\\"popup-image\\" href=\\"images\/docs\/\1\\"><img src=\\"images\/docs\/tn.\1/g' test.txt
Output from Sed:
<a class=\"popup-image\" href=\"images/docs/m\"><img src=\"images/docs/tn.mydash_grooms.png\" alt=\"\" />
Why is my backreference not working properly both times used?
Trying to accomplish:
Changing:
<img src=\"images/docs/mydash_grooms.png\" alt=\"\" />
to
<a class=\"popup-image\" href=\"images/docs/mydash_grooms.png\"><img src=\"images/docs/tn.mydash_grooms.png\" alt=\"\" />
You have to escape the \ so they become actually "\\". However, you also have to escape the /, which makes the string very complex. I suggest replacing the delimiter of sed (i.e., the '/'), to another character to avoid complex strings. For example, using #
sed 's#<img src=\\"images/docs/\(.*\)\\" alt=\\"\\" />#<a class=\\"popup-image\\" href=\\"images/docs/\1\\"><img src=\\"images/docs/tn.\1\\" alt=\\"\\" />#g' test.txt
Futhermore, please replace the [[:graph:]], it was not working for me.
This might work for you (GNU sed):
sed -r 'h;s|img src(.*) alt.*|a class=\\"popup-image\\" href\1>|;G;s/\n//;s|(.*/)([^>])|\1tn.\2|' file
Save the line in the hold space then alter the line to replicate the first attribute. Append the original line and insert the tn. into the file name.

Resources