例如(.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)