如何將 .bash_history 檔案中的紀元時間(例如#214235235)和逗號合併並將其保存在一行中。

如何將 .bash_history 檔案中的紀元時間(例如#214235235)和逗號合併並將其保存在一行中。

例如(.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 或任何新檔案)
  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)

相關內容