我正在尝试查看是否有更简单的方法从字符串开始到结束字符以识别它所具有的“日期格式”,我有一个解决方案正在运行,但我在寻找是否有更方便的做法。
EXAMPLE:
file_name=file_test_<yyyymmdd>_format.xls
SUBSTRING FROM "<" to ">"
<yyyymmdd>
这就是我所拥有的并且正在发挥作用:
file_name="test_file_<mmddyyyy>_test"
format_date=""
Char_Found=0
index=0
while [ $index -lt ${#file_name} ]; do
letter=$(echo $file_name | cut -c$(( $index + 1 )))
if [ $letter == "<" ]; then
Char_Found=1
fi
if [ $letter == ">" ]; then
format_date=${format_date}">"
break
fi
if (( ${Char_Found} )); then
format_date=${format_date}"${letter}"
fi
index=$(( $index + 1 ))
done
echo "Format string: ${format_date}"
#OUTPUT: <mmddyyyy>
答案 0 :(得分:1)
发布的实施不仅冗长,
由于在循环中调用SELECT DATE(tours.start) AS acum_date,
COUNT(tour_user.user_id) AS guide_assignments,
A.tour_count
FROM `tour_user`
LEFT JOIN `tours` ON `tours`.`id` = `tour_user`.`tour_id`
LEFT JOIN
(SELECT DATE(tours.start) AS tour_date,
COUNT(DISTINCT tours.id) AS tour_count
FROM tours
GROUP BY DATE(tours.start)) AS A ON `A`.`tour_date` = DATE(tours.start)
GROUP BY `acum_date`
ORDER BY `acum_date` ASC
,效率非常低,
为每个角色。
在Bash中使用所谓的参数扩展可以实现更简单,更有效的解决方案, 这似乎也适用于ksh:
cut
输出:
file_name="test_file_<mmddyyyy>_test" format_date=$file_name format_date="<${format_date#*<}" # chop off the beginning until < format_date="${format_date%%>*}>" # chop off the end from > echo "Format string: ${format_date}"