빈 줄 뒤의 개행 제거

빈 줄 뒤의 개행 제거

데이터

4. Alendronic acid
A. Antiosteoporotic agent. 
B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. 
C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline. 
B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles. 
C. Last option of asthma attack, COPD, Reversible airways obstruction. 

내가 원하는 것(아래 설명된 의사 코드에 표시된 것처럼 나중에 빈 줄이 없음)

4. Alendronic acid
A. Antiosteoporotic agent. B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline. B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles. C. Last option of asthma attack, COPD, Reversible airways obstruction. 

내 시도는 원래 빈 줄을 모두 제거한다는 아이디어를 기반으로 했지만 gsed -n "s/^$//;t;p;"지금은 불가능합니다.

의사코드

  • (빈 줄이 아닌) 모든 줄 바꿈을 제거합니다. tr '\n' ' '(이제 모든 것이 라이너이지만 문제는 빈 줄도 필요하기 때문입니다!)
  • 모두 다 바꿔ㅏ.통과\nA.통과sed 's#A.#\nA.#'
  • 빈 줄을 모두 제거하세요.gsed -n "s/^$//;t;p;"

의사코드 요약

cat                                 \
     10.6.2015.tex                  \
                                    \
| tr '\n' ' '                       \
                                    \
| sed 's#A.#\nA.#'                  \
                                    \
| gsed -n "s/^$//;t;p;"             \
                                    \
> 10.6.2015_quizlet.tex

그러나 이는 첫 번째 줄의 논리적 오류로 인해 잘못된 것입니다.

Perl/Sed/tr에서 빈 줄 뒤의 개행 문자를 제거하는 방법은 무엇입니까?

답변1

Perl이나 awk를 사용하여 한 번에 한 섹션씩 데이터를 읽고 첫 번째 개행 문자를 제외한 모든 항목을 제거합니다.

perl -00 -pe '$\="\n\n"; s/\n/\0/; s/\n//g; s/\0/\n/' file

댓글을 달았습니다.

perl -00 -pe '   # each record is separated by blank lines (-00)
                 # read the file a record at a time and auto-print (-p)
    $\="\n\n";   # auto-append 2 newlines to each record
    s/\n/\0/;    # turn the first newline into a null byte
    s/\n//g;     # remove all other newlines
    s/\0/\n/     # restore the first newline
' file

비슷하게

awk -v RS= -F'\n' '{print $1; for (i=2; i<=NF; i++) printf "%s", $i; print ""; print ""}' file

답변2

당신은 그것을 사용할 수 있습니다 :

sed '/[0-9]\./{n;:l;N;/\n$/!s/\n/ /;t l}' file

그러면 다음이 출력됩니다.

4. Alendronic acid
A. Antiosteoporotic agent.  B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass.  C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline.  B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles.  C. Last option of asthma attack, COPD, Reversible airways obstruction. 

설명하다

숫자와 마침표가 있는 행을 와 일치시킵니다 /[0-9]\./. 그런 다음 다음 줄로 이동하는 코드 블록을 입력합니다 n. 루프를 시작하고 :l다음 줄을 로 추가 N하고 개행 문자를 로 바꿉니다 s/\n/ /. 조건에 의해 선택된 빈 줄에 도달하면 루프가 종료됩니다 /\n$/!.

답변3

다음은 awk입력 및 출력에 대한 필드 및 레코드 구분 기호를 적절하게 정의하여 문제를 해결하는 솔루션입니다. 따라서 유효한 명령( $1=$1 FS)은 매우 간단합니다.

awk '
  BEGIN { RS="" ; FS="\n" ; OFS="" ; ORS="\n\n" }
  $1=$1 FS
'

설명하다:

RS=""- 빈 줄로 구분된 데이터 블록을 하나의 레코드로 처리합니다.

FS="\n"- 블록의 각 행을 자체 주소 지정 가능 필드로 정의

OFS=""- 공백으로 끝나는 데이터로 인해 필드 구분자를 출력할 필요가 없습니다.

ORS="\n\n"- 새 블록(입력 데이터)을 빈 줄로 구분합니다.

$1=$1 FS- 첫 번째 필드(즉, 첫 번째 줄)는 개행 문자로 블록의 나머지 줄과 구분됩니다. 왜냐하면 할당이 awk수정된 레코드(블록)에서 참 조건이기 때문에 인쇄됩니다.

답변4

sed -n '/^[0-9]/!H;//x;$x;s/\n\([^A]\)/ \1/gp' <infile >outfile

문제를 해결하는 것 같습니다.

  1. /^[0-9]/!H
    • 줄이 숫자로 시작하지 않으면 새 줄 뒤의 이전 공백 !에 추가됩니다 .H\n
  2. //x;$x
    • 숫자로 시작하거나 이것이 $마지막 줄인 경우 x패턴과 h이전 공백을 변경하세요.
  3. s/\n\([^A]\)/ \1/gp
    • 하나 이상의 ewline 시퀀스 \n다음에 비-문자가 오는 경우문자는 패턴 공간에서 찾은 다음 이러한 시퀀스의 줄눈으로 전역적으로 g대체 s///될 수 있습니다.\n<스페이스>그리고 p결과를 인쇄해 보세요.
    • \newline이 발견되는 유일한 시간은 e가 변경된 직후입니다 x. 따라서 숫자로 시작하는 줄이나 마지막 줄에서만 찾을 수 있습니다.
    • \n구분선이 다음과 같기 때문에 선행 숫자는 선행 가장자리를 유지합니다.마지막e가 x변경되면 패턴 공간의 문자가 \([^A]\)뒤따라오므로 s///공백으로 대체되지 않습니다.

그게 다야.

산출:

4. Alendronic acid
A. Antiosteoporotic agent.  B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass.  C. Osteoporosis in combination with vitamin D. 

5. Aminophylline
A. Methylxanthine. Less potent and shorter-acting bronchodilator than Theophylline.  B. Phosphodiesterase (PDE) inhibitor, so increase cAMP so affecting calcium so relaxes respiratory SM and dilates bronchi/bronchioles.  C. Last option of asthma attack, COPD, Reversible airways obstruction. 

관련 정보