주어진 규칙에 따라 텍스트 줄을 들여쓰기합니다.

주어진 규칙에 따라 텍스트 줄을 들여쓰기합니다.

사용자 정의 규칙에 따라 시의 줄을 점점 더 많이 들여쓰는 방법을 알고 싶습니다. 예를 들어

다음과 같은 결과가 있다고 가정합니다.

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.

우리는 다음과 같이 첫 번째 줄 뒤의 세 줄에 3개의 공백을 추가하여 재귀적으로 들여쓰기를 원합니다.

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

이것을 달성하는 가장 쉬운 방법은 무엇입니까?

답변1

첫 번째 줄만 들여쓰기하고 네 번째 줄마다 들여쓰려면 다음을 사용할 수 있습니다 awk.


$ awk 'NR % 4 != 1{$0="    "$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,
    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.

또는 perl:

$ perl -pe 's/^/    / if $. % 4 != 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,
    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.

두 경우 모두 현재 줄 번호가 다음 줄인 경우 줄 시작 부분에 4개의 공백을 추가합니다.기준 치수4는 1과 같지 않습니다. 즉, 첫 번째, 네 번째 등을 제외한 모든 행에 대해 이 작업을 수행한다는 의미입니다.

awk에서는 NR줄 번호 $0와 줄의 내용이므로 NR % 4 != 1{$0=" "$0};"현재 줄 번호 모듈로 4가 1이 아닐 때 줄 시작 부분에 공백 4개를 추가한다"는 뜻이다. 마지막 것은 1;"인쇄"의 약자입니다.

Perl에서 는 $.현재 줄 번호이며 의 s/old/new/첫 번째 항목을 대체하는 대체 연산자입니다 . 이는 "현재 줄 번호 모듈로 4가 1이 아닌 경우 줄의 시작 부분( )을 공백 4개로 바꾸십시오"를 의미합니다. "제공된 스크립트를 적용한 후 입력 파일의 각 줄을 인쇄합니다"를 의미합니다 .oldnews/^/ / if $. % 4 != 1^-p-e

다음은 완전히 동일한 perl 명령이지만 더 자세하고 이해하기 쉬운 버전입니다.

perl -e '
open(my $fileHandle, "<", $ARGV[0]);
my $lineCount=0;

while(my $line = <$fileHandle>){
   $lineCount += 1;
   if ( $lineCount % 4 != 1 ){
       ## or $line = "    " . $line
       $line =~ s/^/    /
   }
   print "$line";
}' file

또는 거의 동일합니다.

perl -e '
open(my $fileHandle, "<", $ARGV[0]);
my $lineCount=0;

while(my $line = <$fileHandle>){
   $lineCount += 1;
   unless ( $lineCount % 4 == 1 ){
       $line = "    " . $line
   }
   print "$line";
}' file

답변2

사용행복하다(이전 Perl_6)

자동 인쇄(아래 처음 두 개):

raku -pe 's/^/    / if $++ % 4;'  

또는

raku -pe 'state $i; s/^/    / if $i++ % 4;'  

또는 (비자동 인쇄, 다음 두 가지)

raku -ne '$++ % 4 ?? put "    $_" !! put "$_";' 

또는(비자동 인쇄, 내부 인용 없음)

raku -ne 'state $i; $i++ % 4 ?? qq[    $_].put !! $_.put;' 

위의 Raku 코드 중 처음 두 예제는 -pe@terdon의 Perl(5) 한 줄짜리 코드(플래그)를 직접 번역한 것입니다. Raku는 $_Perl과 동일한 테마 변수를 사용합니다. Raku는 state한 번만 초기화되는 변수를 선호하여 많은 특수 변수를 포기합니다 . 일반적으로 사용되는 state변수에는 (여기서 줄 번호를 계산하는 데 사용되는 익명 스칼라) $와 같은 증가/감소 형식이 포함됩니다.$++

마지막 두 가지 예는 비자동 인쇄( -ne로고)이며 Raku의 "?? 진짜 !! 잘못된" 삼항 연산자. 이러한 형식은 개념적으로 @Ed_Morton의 awk코드(주석)와 유사합니다. Raku 삼항의 각 절반에 병렬 텍스트를 유지하려고 시도하지만 실제로는 $_주제 변수를 출력할 때 해야 할 일은 작성하는 것뿐입니다 .put(Raku 기본값은 테마 변수입니다) . 명시적으로 지정하지 않은 경우 $_) 또한 putRaku에는 \n줄 바꿈이 추가됩니다 .

마지막으로 Raku의 많은 인용 문제는 아래와 같이 "Q 언어"를 사용하여 해결됩니다(위 4행의 사용 예 qq[…]).

출력 예(위의 4개 줄 모두):

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.

https://docs.raku.org/syntax/state
https://docs.raku.org/언어/quoting
https://docs.raku.org/언어/operators#index-entry-operator_ternary

관련 정보