I am not sure how to format strings that im looking for that contain spaces. When i look for a string in format 21 Jun 2017
${date_to_search_for}= Convert Date ${completion_date} date_format=%Y-%m-%d %H:%M:%S.%f result_format=%d %b %Y exclude_millis=True
Wait Until Element Is visible xpath=//*[contains(text(),\"${date_to_search_for}\")]
It gives error
Element 'xpath=//*[contains(text(),"21 Jun 2017")]' was not visible in 30 seconds
Arguments: [ 'xpath=//*[contains(text(),"21 Jun 2017")]' ]
Ive tried escaping the quotes as above and it gives "
When i dont escape them, it gives \"
Any ideas?
no need to trim spaces when searching xPath. Please check original html element, does it include any other elements (not only text you are searching for)? if yes - change locator to
//*[contains(. ,'21 Jun 2017')]
Related
I understand how to reformat a date using the date command, and I am fine with that. However, I have a wrinkle in that I am struggling with - the date I want to reformat is the output of another command, so I am storing it in the variable. I am struggling with the syntax of how to specify that I want to take the output of one command, and run it through date -d, and store it in another variable. Here is what I tried:
expdate=`get_expire_date.sh`
echo $expdate
Mon 23 Mar 2022 05:05:05 PM UTC
expdval=`date -d'($expdate)'`
echo $expdval
I get today's date, not the converted expire date from the script output. If I leave the parenthesis out, of course, it treats $expdate as the literal text to translate and gives an error, whereas if I leave the single quote marks off, it uses the spaces in the date string as a delimiter and only grabs the first token.
What am I doing wrong?
First, parameter expansion doesn't occur inside single quotes. You would need to change the single quotes
expdval=`date -d'($expdate)'`
to double quotes
expdval=`date -d"($expdate)"`
Second, the parentheses create an invalid input, which results (for reasons I don't really understand) in an output of midnight of the current day. (You'll get the same result with the trivial invalid date date -d "".)
Drop the parentheses, and you'll get the same date back (because the input format matches the default output format).
$ date -d "$expdate"
Wed Mar 23 13:05:05 EDT 2022
To actually manipulate it, you'll need an explicit output format:
$ date -d "$expdate" +%Y-%m-%d
2022-03-23
or some form of date "arithmetic":
$ date -d "$expdate + 2 days"
Fri Mar 25 13:05:05 EDT 2022
I found I had to use double-quotes instead, like this (and sorry for the old way of doing things, updating to new shell syntax):
expdval=$(date -d"$(get_expire_date.sh)")
date +'%A %B %d' | sed -e 's/\(^\|[^[:digit:]]\+\)0\+\([[:digit:]]\)/\1\2/g
I like the output of the above command, which strips leading zeroes off days of the month produced by the date command, in the case of numerals less than 10. It's the only way I've thus far found of producing single digit dates from the date command's output for the day of the month, which otherwise would be 01, 02, 03, etc.
A couple of questions in this regard. Is there a more elegant way of accomplishing the stated goal of stripping off zeroes? I do know about date's %e switch and would like to use it, but with numerals 10 and greater it has the undesirable effect of losing the space between the month name and the date (so, July 2 but July10).
The second question regards the larger intended goal of arriving at such an incantation. I'm putting together a script that will scrape some data from a web page. The best way of locating the target data on the page is by searching on the current date. But the site uses only single digits for the first 9 days of the month, thus the need to strip off leading zeroes. So what's the best way of getting this complex command into a variable so I can call it within my script? Would a variable within a variable be called for here?
RESOLUTION
I'll sort of answer my own question here, though it is really input from Renaud Pacalett (below) that enabled me to resolve the matter. His input revealed to me that I'd not understood very well the man page, particularly the part where is says "date pads numeric fields with zeroes," and below that where it is written "- (hyphen) do not pad the field." Had I understood better those statements, I would have realized that there is no need for the complex sed line through which I piped the date output in the title of this posting: had I used there %-d instead of just %d there would have been no leading zeroes in front of numerals less than 10 and so no need to call sed (or tr, as suggested below by LMC) to strip them off. In light of that, the answer to the second question about putting that incantation into a variable becomes elementary: var=$(date +'%A %B %-d') is all that is needed.
I may go ahead and mark Renaud Pacalet's response as the solution since, even though I did not implement all of his suggestions into the latest incarnation of my script, it proved crucial in clarifying key requirements of the task.
If your date utility supports it (the one from GNU coreutils does) you can use:
date +'%A %B %-d'
The - tells date to not pad the numeric field. Demo:
$ date -d"2021/07/01" +'%A %B %-d'
Thursday July 1
Not sure I understand your second question but if you want to pass this command to a shell script (I do not really understand why you would do that), you can use the eval shell command:
$ cat foo.sh
#!/usr/bin/env bash
foo="$(eval "$1")"
echo "$foo"
$ ./foo.sh 'date -d"2021/07/01" +"%A %B %-d"'
Thursday July 1
Please pay attention to the double (") and simple (') quotes usage. And of course, you will have to add to this example script what is needed to handle errors, avoid misuses...
Note that many string comparison utilities support one form or another of extended regular expressions. So getting rid of these leading zeros or spaces can be as easy as:
grep -E 'Thursday\s+July\s+0*1' foo.txt
This would match any line of foo.txt containing
Thursday<1 or more spaces>July<1 or more spaces><0 or more zeros>1
Is there any way to use standard tools (not programming scripts) to parse the date in custom, odd format?
I've got a start script (bash) that should handle the output of the program, that contains dates with very odd formatting (like Jan, 4, 2021, 1:20:30 PM - which would be a pattern like "MMM, d, yyyy, h:mm:ss a".
It would be possible to extract the tokens with sed or awk, but processing them is a nightmare, especially month shortcuts (they are in the same language like system, but that language can be installation-specific) or hours (need to check AM/PM token and add 12 if it's PM).
'date' apparently doesn't support that, but maybe some shell extension or toolkit package? Or I'm out of luck and I need to parse this crappy format token by token with sed/cut/awk?
This what I've tried was to do touch -d "Date" after removing the commas (so that I can compare dates with [[ file1 -ot file2 ]], but the problem is, that touch has ignored the TIME part, and ls -lh has shown, that the year was set in place of time, and the result of the comparison was therefore invalid.
Convert date with GNU date and bash:
d="Jan, 4, 2021, 1:20:30 PM"
IFS=',:' read mon day year hour min sec a <<<"$d"
date -d "$mon $day $year $hour:$min:$sec $a" '+%Y-%m-%d %H:%M:%S'
Output:
2021-01-04 13:20:30
I'm retrieving a list of files but the parser is breaking on one of them. So I'm going in to modify it:
RegEx:
/ +/g
Here is a listing it is having trouble with:
drw-rw---- 2 joeUser dhapache 7 May 18 2011 Hello World.html
Is there a RegEx people use to parse file lists? Also, is it OK to assume that anything after the 9th position is the filename (where you are splitting on space characters?
i'm using this regex :
^([\-ld])([\-rwxs]{9})\s+(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+(\w{3}\s+\d{1,2}\s+(?:\d{1,2}:\d{1,2}|\d{4}))\s+(.+)$
i tested that on both DART (add r at the start of regex) and JS and it works well.
here is the result:
https://regex101.com/r/8osTeQ/1
you can iterate like this:
//code in dart
String s = "drw-rw---- 2 owner group 7 May 12 11:30 index.html";
Iterable<Match> matches = regExp.allMatches(s);
for (Match match in matches) {
print("${match.group(1)}\n");//type file or dir
print("${match.group(2)}\n");//permission
print("${match.group(3)}\n");//nb files
print("${match.group(4)}\n");//owner
print("${match.group(5)}\n");//group
print("${match.group(6)}\n");//size
print("${match.group(7)}\n");//date
print("${match.group(8)}\n");//file/dir name
}
If possible, use MLSD FTP command that has a fixed format like:
type=file;modify=20150803062903;size=1410887680; filename.avi
See RFC 3659.
Fallback to the LIST command only as the last resort for servers that do not support it (like IIS).
If you need file names only, use NLIST command. All servers do support it and it produces file names only, so no parsing is needed.
There's no defined format for the LIST command. So it's not safe to assume anything.
Particularly the IIS uses a completely different format:
02-11-15 03:05PM 1410887680 movie.avi
Even for servers that use a Unix-like format, the format will differ with age of the file. If the file is new enough, its time format will be like:
-rw-r--r-- 1 ftp ftp 11 Sep 09 16:00 new_file.txt
It's also pretty common for the user and group columns to blend together if the username is too long.
If it were me, I'd ensure that your return data is formatted to fill a given number of characters per field, padded where needed with spaces. Then my RegEx would be something like:
^(.*{12})(.*{3})(.*{20})(.*{20})(.*{3})(.*{12})(.*{30})
Where:
\1 = permissions
[...]
\6 = date
\7 = filename
Once you have the values for each match/group, trim the resulting data.
Make sense?
I have a csv file formatted like this:
Wed Mar 07 00:00:00 CET 2012,78.0
Thu Mar 08 00:00:00 CET 2012,46.0
...
I read it using standard input, and tried to match parts of each line using:
ARGF.each_line do |line|
time << line.scan(/^\w{3} (\w{3}) (\d{2}) (\d{2}:\d{2}:\d{2}) \w+ (\d{4}),([.\d]+)$/)
end
Which for some reason only returns the last line in the file.
If I copy the CSV file to a string variable, it starts to match each line correctly. If I remove the dollar sign from the regex it matches correctly as well, but I don't understand why $ doesn't work when reading from ARGF. Any ideas?
Is there a reason you have to use ARGF? You can check out the CSV class in the standard library, which gives tools to make the processing easier.
Here's an example that yields one row at a time to foreach. I would guess that this allows you to not worry about where lines begin or end:
require "csv"
CSV.foreach("path/to/file.csv") do |row|
time << line.scan(/^\w{3} (\w{3}) (\d{2}) (\d{2}:\d{2}:\d{2}) \w+ (\d{4}),([.\d]+)$/)
end