내 목표는 다음과 같이 시의 줄 번호를 매기는 데 사용할 수 있는 일반적인 텍스트 유틸리티가 Linux에 있는지 확인하는 것입니다.
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.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.
이것을 얻었습니다:
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
12 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
16 Things unattempted yet in Prose or Rhime.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
20 Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
24 That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.
awk도 이런 식으로 줄을 들여쓸 수 있습니다
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,
이런 것도 있나요?
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,
답변1
awk
아마도 가장 적합한 도구 일 것입니다.
$ 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,
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
12 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
16 Things unattempted yet in Prose or Rhime.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
20 Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
24 That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.
현재 입력 파일( )의 라인 번호가 FNR
4로 나누어질 때마다 라인 번호를 사용하여 라인을 인쇄합니다. 그렇지 않으면 같은 수의 공백을 들여쓰기하여 인쇄합니다.
추가로 들여쓰기하려면 다음을 시도해 보세요.
awk 'FNR % 4 == 0 { printf "%6i %s\n", FNR, $0 ; next };
NR % 4 == 1 || NR % 4 == 3 { printf "%6s %s\n", "", $0 ; next };
{printf "%6s %s\n", "", $0}' poem.txt
%6s
두 번째 줄 뒤에 추가로 두 개의 공백이 있음을 확인하십시오.
그리고 두 번째 들여쓰기 스타일의 경우:
awk 'FNR % 4 == 0 { printf "%6i %s\n", FNR, $0 ; next };
FNR % 4 == 2 { printf "%6s %s\n", "", $0 ; next };
{printf "%6s %s\n", "", $0}' poem.txt
여기서 추가 공백은 %6i
첫 번째 줄과 %6s
두 번째 줄 뒤에 있습니다.
답변2
Raku(이전 Perl_6) 사용
raku -e 'my @lines1 = lines>>.trim; for @lines1.kv -> $k,$v { put ($k+1) %% 4 \
?? sprintf("%6s ", $k+1) ~ $v !! sprintf("%6s ", "") ~ $v};' 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.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.
샘플 출력:
잃어버린 낙원, 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
12 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
16 Things unattempted yet in Prose or Rhime.
And chiefly Thou, O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
20 Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumin, what is low raise and support;
24 That to the highth of this great Argument
I may assert Eternal Providence,
And justifie the wayes of God to men.
위 내용은 Perl 계열의 프로그래밍 언어인 Raku로 작성된 답변입니다. 즉, lines
라쿠의 루틴(자동 씹기)을 사용하여 시를 소리내어 읽으며 모든 행은 trim
-med입니다. 데이터는 배열에 저장됩니다 @lines1
.
다음 문( for
루프) 에서는 @lines1
배열이 kv
키-값 쌍으로 변환되고 반복됩니다. 루프 블록은 Raku의 기능을 구현합니다.삼항 연산자. True ($k+1) %% 4
인 경우 각 항목은 섹션의 왼쪽에 ??
배치되고 , 그렇지 않은 경우 False인 경우 빈 문자열이 배치됩니다 .$k+1
$v
!!
""
바라보다이 답변시가 단락/섹션으로 구분되는 경우와 같이 줄 번호 매기기를 다시 시작하는 방법을 알아보세요.