
例如(.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
- 這應該複製到另一個檔案中,例如(newfile.txt 或任何新檔案)
- 該文件應該忽略沒有紀元時間的命令
- 並且應該忽略沒有命令的紀元時間
即 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)