파일에서 두 번째 줄마다 삭제하는 방법? table. Total count 13 No

파일:

Data inserted into table. Total count 13
No error occurred
Data inserted into table. Total count 45
No error occurred
Data inserted into table. Total count 14
No error occurred
Data inserted into table. Total count 90
No error occurred

예상 출력 파일 :

Data inserted into table. Total count 13
Data inserted into table. Total count 45
Data inserted into table. Total count 14
Data inserted into table. Total count 90

출력이 이런 식으로 보이기를 원합니다. 두 번째 줄은 모두 삭제되지만 줄 사이에는 간격이 없습니다.



답변

sed:

sed -e n\;d <file

POSIX로 awk:

awk 'FNR%2' <file

나이가 든 경우 awk(예 oawk🙂 :

oawk 'NR%2 == 1' <file

ex:

$ ex file <<\EX
:g/$/+d
:wq!
EX

파일을 그 자리에서 편집합니다.

  • g 세계적인 명령을 표시하다
  • /$/ 모든 라인과 일치
  • +d 다음 줄을 삭제
  • wq! 모든 변경 사항을 저장하십시오

이 접근 방식은 접근 방식과 동일한 이상적인 방식을 공유하고 sed현재 줄의 모든 다음 줄을 줄 1부터 삭제합니다.

perl:

perl -ne 'print if $. % 2' <file

그리고 perl6:

perl6 -ne '.say if $*IN.ins % 2' <file
perl6 -ne '.say if ++$ % 2' <file


답변

두 번째 줄을 모두 삭제하여이 문제를 해결하면 오류가 발생하기 쉽습니다 (예 : 프로세스가 때때로 두 개의 의미있는 줄을 생성하는 경우). 가비지를 걸러내는 것이 좋습니다.

grep -v "No error occurred" file

필터로 실행할 수 있으며 여기에 더 많은 가비지 패턴을 추가하고 결과를 향상시킬 수 있습니다.


답변

GNU로 질문에 대해 sed:

sed '0~2d' file

두 번째 줄마다 삭제하지만 내용별로 필터 줄을 제공하고 싶습니다.

sed '/Data/! d' file

또는 같은 결과

sed '/No error/d' file


답변

사용하는 방법은 다음과 같습니다 sed.

sed -n 'p;n' filename

GNU의 또 다른 방법 sed:

sed -n '1~2p' filename

위 명령의 출력 :

Data inserted into table. Total count 13
Data inserted into table. Total count 45
Data inserted into table. Total count 14
Data inserted into table. Total count 90


답변

당신은 시도 할 수 있습니다 awk:

awk 'NR % 2 != 0' file

또는 다음을 포함하는 줄만 인쇄 할 수 있습니다 Data inserted.

awk '$0 ~ /Data inserted/' file


답변

또 다른 대답은 vi / vim을 사용할 수 있다는 것입니다.

qdjddq

그리고 파일이 500 줄 (예 :)이라면

250 @ d

그런 다음 쓰기 및 종료 유형

:엑스

또는 문제가 발생하여 저장하고 싶지 않은 경우 :

:큐!

설명:

q      #Start Recording
 d     #Put the recording into register 'd'
  j    #Move the cursor down
   dd  #Delete the line
     q #Stop recording

250    #Number of repeats
   @d  #Playback the recording in register 'd'.


답변

다른 방법으로 수행 할 수 있습니다.

< file paste - - | cut -f1

홀수 라인에 탭이 포함되어 있지 않다고 가정합니다. 그렇다면 다른 구분 문자를 선택해야합니다 (예 : :여기).

< file paste -d: - - | cut -d: -f1