.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)

관련 정보