エポックタイム (例: 214235235) と .bash_history ファイルのコマンドを組み合わせて 1 行に保存する方法。エポックタイムを含むコマンドのみが表示されます。

エポックタイム (例: 214235235) と .bash_history ファイルのコマンドを組み合わせて 1 行に保存する方法。エポックタイムを含むコマンドのみが表示されます。

例: (.bash_history ファイル)

cd Fortigate_Report/
ll
exit
#1512031841
history>set1
#1512031849
history>set2
#1512031864
vi comm -23 <(sort set1) <(sort set2)
#1512031877
comm -23 <(sort set1) <(sort set2)
#1512031892
comm -23 <(sort set2) <(sort set1)
#1512031971
  1. これを別のファイルにコピーする必要があります(例:newfile.txt または任意の newfile)。
  2. ファイルはエポックタイムのないコマンドを無視するべきである
  3. そして、コマンドのないエポックタイムを無視するべきである

つまり、newfileの出力は次のようになります。

#1512031841 history>set1 
#1512031849 history>set2
#1512031864 vi comm -23 <(sort set1) <(sort set2)
#1512031877 comm -23 <(sort set1) <(sort set2)
#1512031892 comm -23 <(sort set2) <(sort set1)

答え1

sed

sed -n '/^#[0-9]\{1,\}$/N;s/\n/ /p' < file > newfile

答え2

testdataサンプルデータを印刷します...

$ testdata() { cat<<dog                        
cd Fortigate_Report/
ll
exit
#1512031841
history>set1
#1512031849
history>set2
#1512031864
vi comm -23 <(sort set1) <(sort set2)
#1512031877
comm -23 <(sort set1) <(sort set2)
#1512031892
comm -23 <(sort set2) <(sort set1)
#1512031971
dog
}

...これは、タイムスタンプが見つかった場合にのみ反応するパイプに渡されますawk。次に、次の行が読み込まれ、前に見つかったタイムスタンプが先頭に付けられて出力されます。

$ testdata | awk '/^#/ && (getline L) > 0 { print $0, L }'
#1512031841 history>set1
#1512031849 history>set2
#1512031864 vi comm -23 <(sort set1) <(sort set2)
#1512031877 comm -23 <(sort set1) <(sort set2)
#1512031892 comm -23 <(sort set2) <(sort set1)

関連情報