grep a time hh:mm:ss

Alb*_*tes 2 linux bash grep

我需要将一个给定的日期格式(hh:mm:ss)分成如:10:3​​0:50的变量,如:hour = 10; 在bash脚本中,minute = 30和seconds = 50 whit grep.

我得到的时间不是一个变量,例如:time ="10:30:50"而我只得到带有以下表达式的秒数:

秒=echo $time | grep -oE "[^:]+$"

获得分钟和小时的任何帮助?

dog*_*ane 8

使用read.它是一个外壳内置命令所以比更有效grep,sed和其它外部命令.

time="10:30:50"
IFS=: read hr min sec <<< "$time"
Run Code Online (Sandbox Code Playgroud)

输出:

$ echo "$hr"
10
$ echo "$min"
30
$  echo "$sec"
50
Run Code Online (Sandbox Code Playgroud)