Bash
重擊時間轉換器
我有一個問題,如何將時間從 12h AM/PM 格式轉換為 24h?例如,我有來自標準輸入的輸入,一些文本是這樣的:
The event starts at 03:25PM and is expected to end at 06:17PM. Registration will be opened from 09:00AM until 06:00 PM. The event starts at 15:25 and is expected to end at 18:17. Registration will be opened from 09:00 until 06:00 PM.
如何讀取所有 hh:mmPM 和 hh:mmAM 並進行轉換?但我不想像這樣使用 awk 或 smth。我如何只使用 bash 循環、if/else、echo、sed 等來做到這一點?謝謝!
生成替換數字的 sed 腳本:
#! /bin/bash for pm in {0..11} ; do (( h = pm + 12 )) pm=$(printf %02d $pm) echo "s/$pm\(:[0-9][0-9]\) \?PM/$h\1/g" done | sed -e 's/ \?AM//g' -f- input.txt
該循環產生以下腳本:
s/00\(:[0-9][0-9]\) \?PM/12\1/g s/01\(:[0-9][0-9]\) \?PM/13\1/g s/02\(:[0-9][0-9]\) \?PM/14\1/g s/03\(:[0-9][0-9]\) \?PM/15\1/g s/04\(:[0-9][0-9]\) \?PM/16\1/g s/05\(:[0-9][0-9]\) \?PM/17\1/g s/06\(:[0-9][0-9]\) \?PM/18\1/g s/07\(:[0-9][0-9]\) \?PM/19\1/g s/08\(:[0-9][0-9]\) \?PM/20\1/g s/09\(:[0-9][0-9]\) \?PM/21\1/g s/10\(:[0-9][0-9]\) \?PM/22\1/g s/11\(:[0-9][0-9]\) \?PM/23\1/g
多餘的表情被移除
AM
,同時保持時間不變。