これはここに与えられた非常に興味深い答えに関連しています。特定の数字を省略する時の行番号付けのためのテキストユーティリティやハッキングはありますか?
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.
空白の後ろの4行について行番号付けを再開できるかどうか疑問に思います。
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
サンプル入力:
失われた楽園、ジョンミルトン私(抜粋):
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.
上記は、Perlシリーズのプログラミング言語であるRakuで書かれた答えです。つまり、2つ以上の連続する改行が発生すると、詩が編集され、データが配列に保存さslurp
れます(これは本質的に詩を「段落」に分割します)。 2番目のステートメントでは、スーパー演算子を使用して、右側の関数を使用して左側の各要素を変更します。だからチェーンを読んで、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