バッシュタイムコンバータ

バッシュタイムコンバータ

質問があります。午前/午後12時間形式を24時間形式に変換するには?たとえば、標準入力に次のテキストを入力しました。

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やそのようなものを使用したくありません。 bashループ、if / else、echo、sedなどを使用してこれを行うにはどうすればよいですか?ありがとうございます!

ベストアンサー1

代替番号を生成する 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時間はそのまま残り、重複した式を削除します。

おすすめ記事