パターン検索とファイルの値の変更

パターン検索とファイルの値の変更

/home/digadm02/.bash_history:#1520325239 /home/digadm02/.bash_history:sudo su

test.txt ファイルに次の行があります。 "#1520325239" パターンを検索し、"date -d @1520325239" コマンドで取得した値に置き換える必要があります。このようなラインがたくさんあります。各行を読み、同じファイルに置き換える必要があります。

ベストアンサー1

よく理解している場合、タイムスタンプは1つではなく、同様の行が複数あることです。このスクリプトは必要な代替操作を実行する必要があります。

#!/bin/bash

# get timestamps from the file
dates=`sed -e '1,$s/^.*#\(.*\) \/.*/\1/' /tmp/test.txt`

for date in $dates
do
    # get human readable format from timestamp
    newdate=`date -d @${date}`
    # replace timestamp with human readable date
    sed -i "1,\$s/\#$date/$newdate/" /tmp/test.txt
done

おすすめ記事