이것은 여기에 주어진 매우 흥미로운 답변과 관련이 있습니다.특정 숫자를 생략하는 시의 줄 번호 매기기를 위한 텍스트 유틸리티나 해킹이 있습니까?
awk 'FNR % 4 == 0 { printf "%6i %s\n", FNR, $0 ; next }; {printf "%6s %s\n", "", $0}' poem.txt
이것은 시의 행에 자동으로 번호를 매기는 작업을 수행합니다.
그러나 다음과 같이 빈 줄로 구분된 여러 개의 시가 있는 경우:
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
공백 뒤의 네 줄에 대해 줄 번호 매기기를 다시 시작할 수 있는지 궁금합니다.
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
4 With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
8 That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
4 Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
8 Things unattempted yet in Prose or Rhime.
답변1
$ awk '
!NF { c=0 }
NF {
sub(/^ +/,"")
$0 = sprintf("%2s %s", (++c%4 ? "" : c), $0)
}
1' file
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
4 With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
8 That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
4 Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
8 Things unattempted yet in Prose or Rhime.
또는 원하는 경우:
$ awk -v RS= -F'\n' '
NR>1 { print ORS }
{
for ( i=1; i<=NF; i++) {
sub(/^ +/,"",$i)
printf "%2s %s\n", (i%4 ? "" : i), $i
}
}
' file
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
4 With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
8 That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
4 Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
8 Things unattempted yet in Prose or Rhime.
답변2
사용행복하다(이전 Perl_6)
raku -e 'my @para = .split( /\n**2..*/, :skip-empty) given slurp;
@para = @para>>.lines>>.trim; for @para {for $_.kv -> $k,$v {
put ($k+1) %% 4 ?? sprintf("%6s ", $k+1) ~ $v !! sprintf("%6s ", "") ~ $v };
"\n".put;}' file
샘플 입력:
잃어버린 낙원, John Milton 저(발췌):
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
샘플 출력:
잃어버린 낙원, John Milton 저(발췌):
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
4 With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
8 That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: or if Sion Hill
Delight thee more, and Siloa's brook that flow'd
4 Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
8 Things unattempted yet in Prose or Rhime.
위 내용은 Perl 계열의 프로그래밍 언어인 Raku로 작성된 답변입니다. 즉, 2개 이상의 연속 줄 바꿈이 발생하면 시가 편집되어 slurp
데이터가 배열에 저장됩니다(이는 본질적으로 시를 "단락"으로 나눕니다). 두 번째 명령문에서는 슈퍼 연산자를 사용하여 오른쪽의 함수를 사용하여 왼쪽의 각 요소를 수정합니다. 그래서 체인을 읽고,split
\n
@para
>>
@para>>.lines>>.trim
"@para의 모든 요소는 줄로 변환되고(즉, 잘린) @para의 모든 줄은 잘린 줄로 변환됩니다."
다음 문( for
루프) 에서는 @para
배열이 반복됩니다. 각 (단락) 요소는 즉시 kv
키-값 쌍 으로 변환되고 for
Raku의 기능을 구현하는 중첩 루프를 사용하여 반복됩니다.삼항 연산자. True ($k+1) %% 4
인 경우 각 항목은 섹션의 왼쪽에 ??
배치되고 , 그렇지 않은 경우 False인 경우 빈 문자열이 배치됩니다 . 마지막으로 다시 개행 문자로 구분됩니다.$k+1
$v
!!
""
@para
\n
답변3
빈 행이 두 개 이상 있을 때만 카운터를 재설정하는 것이 목표라면 다음을 수행하세요.
awk '{ if(/^$/&& pre==$0)c=0; else c++;
pre=$0;
printf "%6s %s\n", (c&&c%4==0?c:"") , $0
}' infile