My xpath is returning either
John Hernandez | 12 Jul 2012 or 13 July 2012 as string.
If string contains character | then I want string after | otherwise I want the whole string as it is.
I used following xpath :
substring-after(string(./div/span[2]),'|')
Its giving me correct output 12 Jul 2012 if string contains | but if my string does not contains | its returning null. Is there any way to check if string contains | then use string after | o.w return whole string.
Can anyone help me to do this.
In XPath 1.0 you can use
substring-before(substring-after(concat(string(./div/span[2]), "|", string(./div/span[2]), "|"), "|"), "|")
This first normalizes the strings to John Hernandez | 12 Jul 2012 | John Hernandez | 12 Jul 2012 | or respectively 13 July 2012 | 13 July 2012 | and then always returns the second value in the |-list
(but you should switch to XPath 2 anyways, it is much nicer...)
If you are using XPath 2.0 then this will do:
if (contains(string(div/span[2]), '|'))
then substring-after(string(div/span[2]),'|')
else string(div/span[2])
If you're using XPath 1.0, then you must check the string in XSLT (or whatever you're using) before calling substring-after(). For example:
<xsl:choose>
<xsl:when test="contains(div/span[2], '|')">
<xsl:value-of select="substring-after(string(div/span[2]),'|')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="div/span[2]"/>
</xsl:otherwise>
</xsl:choose>
Related
Want to conver below source to expected Target Table.
Source:
========================
Course | Year | Earning
========================
.NET | 2012 | 10000
Java | 2012 | 20000
.NET | 2012 | 5000
.NET | 2013 | 48000
Java | 2013 | 30000
Expected Output:
=====================
Year | .NET | Java
=====================
2012 | 15000 | 20000
2013 | 48000 | 30000
You can do this like rows to column problem. Here i am assuming you have only .Net and Java courses available. if you have more, you need to add more columns to below transformations.
First use expression transformation with below out ports. They will calculate earning for each course.
java = = IIF(Course ='Java', earning, 0)
dotnet= = IIF(Course ='.Net', earning, 0)
Use an aggregator to calculate those columns.
Year -- input output port with group by
out_java = SUM(java)
out_dotnet = SUM(dotnet)
Link Year, out_java ,out_dotnet to corresponding target columns.
So, whole mapping should look like
SQ --> EXP--> AGG --> Target
I have a file with date field like this.
20|1|124|Mar 19 2016 3:00AM
20|1|144|Mar 19 2016 2:00PM
43|1|146|Mar 19 2016 5:30AM
42|1|158|Mar 19 2016 1:50PM
40|1|15|Mar 19 2016 2:30AM
I want to sort by date field, such that the AM will come before PM. so far I have this:
sort -t"|" -k4 testfile.
But i am not sure how to sort the "AM" and "PM" portion. Any help is appreciated.
You can use:
while read -r; do
IFS='|' read -ra arr <<< "$REPLY"
date -d "${arr[-1]}" "+$REPLY#%s"
done < file | sort -t# -k2 | cut -d# -f1
40|1|15|Mar 19 2016 2:30AM
20|1|124|Mar 19 2016 3:00AM
43|1|146|Mar 19 2016 5:30AM
42|1|158|Mar 19 2016 1:50PM
20|1|144|Mar 19 2016 2:00PM
Using date command we parse last field in your pipe delimited field and add EPOCH value in each line delimited by #. Then using sort we do the sorting by 2nd field (EPOCH value) and finally using cut we discard value after #.
You can use a temporary delimiter (ie |) to make AM/PM a column that can be used as a sort field :
$ cat sourcefile | sed 's/\(.\)M$/|\1M/' | sort -t"|" -k5 -k4 | sed 's/|\(.\)M/\1M/'
40|1|15|Mar 19 2016 2:30AM
20|1|124|Mar 19 2016 3:00AM
43|1|146|Mar 19 2016 5:30AM
42|1|158|Mar 19 2016 1:50PM
20|1|144|Mar 19 2016 2:00PM
I have the following logs (removed unnecessary info) :
Feb 18 11:38:54 Kingston dhcpd: DHCPACK
Feb 18 11:39:01 duxbury /USR/SBIN/CRON[27892]:
Feb 18 17:39:01 ruby /USR/SBIN/CRON[13080]:
How Can I grep for a server name (kingston, ruby or duxbury) while ensuring that date/time info is next to the server name? so for instance I could grep for kingston, and it would return "Feb 18 11:38:54 Kingston dhcpd: DHCPACK" but if only "some data Kingston" (no date/time info) was available, then nothing would be returned. Thanks for the help!
grep -E "^[a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ Kingston"
Assume xpath as below:
xpath : //div[#id='cslGridViewPanelControl']/div/div[2]/div/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td/div
Last div element contains 'Displaying 1 to 30 of 145300'.
I need to store 'Displaying 1 to 30 of 145300' in some variable by using selenium tool.
I tried
command target value
store xpath variable1
echo ${variable1}
It displays as variable1: xpath. But i need variable1: 'Displaying 1 to 30 of 145300'.
Can anyone help me please ?
Try storeText ( locator, variableName )
Then use it ${variableName}
Refer Selenium documentation for storeText
saga told you how to store that string, as you need to store only 145300, below code will help you
store | Displaying 1 to 30 of 145300 | string
store | 1 | delimiter
store | javascript{storedVars['string'].split('of')[storedVars['delimiter']]} | result
echo | ${result}
i'm just wondering how can we use awk to do exact matches.
for eg
$ cal 09 09 2009
September 2009
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
$ cal 09 09 2009 | awk '{day="9"; col=index($0,day); print col }'
17
0
0
11
20
0
8
0
As you can see the above command outputs the index number of all the lines that contain the string/number "9", is there a way to make awk output index number in only the 4th line of cal output above.??? may be an even more elegant solution?
I'm using awk to get the day name using the cal command. here's the whole line of code:
$ dayOfWeek=$(cal $day $month $year | awk '{day='$day'; split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", array); column=index($o,day); dow=int((column+2)/3); print array[dow]}')
The problem with the above code is that if multiple matches are found then i get multiple results, whereas i want it to output only one result.
Thanks!
Limit the call to index() to only those lines which have your "day" surrounded by spaces:
awk -v day=$day 'BEGIN{split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", array)} $0 ~ "\\<"day"\\>"{for(i=1;i<=NF;i++)if($i == day){print array[i]}}'
Proof of Concept
$ cal 02 1956
February 1956
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29
$ day=18; cal 02 1956 | awk -v day=$day 'BEGIN{split("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", array)} $0 ~ "\\<"day"\\>"{for(i=1;i<=NF;i++)if($i == day){print array[i]}}'
Saturday
Update
If all you are looking for is to get the day of the week from a certain date, you should really be using the date command like so:
$ day=9;month=9;year=2009;
$ dayOfWeek=$(date +%A -d "$day/$month/$year")
$ echo $dayOfWeek
Wednesday
you wrote
cal 09 09 2009
I'm not aware of a version of cal that accepts day of month as an input,
only
cal ${mon} (optional) ${year} (optional)
But, that doesn't affect your main issue.
you wrote
is there a way to make awk output index number in only the 4th line of cal output above.?
NR (Num Rec) is your friend
and there are numerous ways to use it.
cal 09 09 2009 | awk 'NR==4{day="9"; col=index($0,day); print col }'
OR
cal 09 09 2009 | awk '{day="9"; if (NR==4) {col=index($0,day); print col } }'
ALSO
In awk, if you have variable assignments that should be used throughout your whole program, then it is better to use the BEGIN section so that the assignment is only performed once. Not a big deal in you example, but why set bad habits ;-)?
HENCE
cal 09 2009 | awk 'BEGIN{day="9"}; NR==4 {col=index($0,day); print col }'
FINALLY
It is not completely clear what problem you are trying to solve. Are you sure you always want to grab line 4? If not, then how do you propose to solve that?
Problems stated as " 1. I am trying to do X. 2. Here is my input. 3. Here is my output. 4. Here is the code that generated that output" are much easier to respond to.
It looks like you're trying to do date calculations. You can be much more robust and general solutions by using the gnu date command. I have seen numerous useful discussions of this tagged as bash, shell, (date?).
I hope this helps.
This is so much easier to do in a language that has time functionality built-in. Tcl is great for that, but many other languages are too:
$ echo 'puts [clock format [clock scan 9/9/2009] -format %a]' | tclsh
Wed
If you want awk to only output for line 4, restrict the rule to line 4:
$ awk 'NR == 4 { ... }'