1

次の dmesg のサンプルがあります。

 throttled log output.
57458] bar 3: test 2 on bar 8 is available
[   19.696163] bar 1403: test on bar 1405 is available
[   19.696167] foo: [   19.696168] bar 3: test 5 on bar 1405 is available
[   19.696178] foo: [   19.696179] bar 1403: test 5 on bar 1405 is available
[   20.928730] foo: [   20.928733] bar 1403: test on bar 1408 is available
[   20.928742] foo: [   20.928745] bar 3: test on bar 1408 is available
[   24.878861] foo: [   25.878861] foo: [   25.878863] bar 1403: bar 802 is present

行内のすべてのタイムスタンプを人間の形式に変換したい( "%d/%m/%Y %H:%M:%S")

注: このシステムdmesg -Tには perl がインストールされていません。私はsedまたはawkを使用したソリューションを好みますが、pythonもオプションです。

この問題に対するいくつかの解決策を見つけましたが、私が必要としているものを完全に解決するものはありません。また、自分のニーズに合わせて変更する方法もわかりません。

awk -F"]" '{"cat /proc/uptime | cut -d \" \" -f 1" | getline st;a=substr( $1,2, length($1) - 1);print strftime("%d/%m/%Y %H:%M:%S",systime()-st+a)" "$0}'

または

sed -n 's/\]//;s/\[//;s/\([^.]\)\.\([^ ]*\)\(.*\)/\1\n\3/p' |  while read first; do    read second;    first=`date +"%d/%m/%Y %H:%M:%S" --date="@$(($seconds - $base + $first))"`;   printf "[%s] %s\n" "$first" "$second";  done

ここには python スクリプトもあります。しかし、私がまったく理解していないいくつかのエラーを出力します。

ありがとう!

4

2 に答える 2

3

dmesg -T次のコードは、結果をシミュレートします。これはシェル内のインライン awk であり、スタンドアロン スクリプトまたはシェル関数として格納できます。

awk -v UPTIME="$( cut -d' ' -f1 /proc/uptime )" '
BEGIN {
    STARTTIME = systime() - UPTIME
}
match($0, /^\[[^\[\]]*\]/) {
    s = substr($0, 2, RLENGTH - 2) + STARTTIME;
    s = strftime("%a %b %d %H:%M:%S %Y", s);
    sub(/^\[[^\[\]]*\]/, "[" s "]", $0);
    print
}
'

提供されるように精度を保証するものではありませんdmesg -Tが、結果を少し近づけます。

于 2021-05-12T13:37:17.963 に答える
1

これはちょっとしたタッチアンドゴーですが、少なくとも何かを操作できるようにする必要があります。

awk '
  {
    # tail will be the part of the line that still requires processing
    tail = $0;                               

    # Read uptime from /proc/uptime and use it to calculate the system
    # start time
    "cat /proc/uptime | cut -d \" \" -f 1" | getline st;
    starttime = systime() - st;

    # while we find matches
    while((start = match(tail, /\[[^[]*\]/)) != 0) {
      # pick the timestamp from the match
      s = substr(tail, start + 1, RLENGTH - 2);

      # shorten the tail accordingly
      tail = substr(tail, start + RLENGTH);

      # format the time to our preference
      t = strftime("%d/%m/%Y %H:%M:%S", starttime + s);

      # substitute it into the original line. [] are replaced with || so
      # the match is not re-replaced in the next iteration.
      sub(/\[[^[]*\]/, "|" t "|", $0);
    }

    # When all matches have been replaced, print the line.
    print $0
  }' foo.txt
于 2014-12-16T19:46:57.727 に答える