Text-Processing
用羅馬數字給詩節編號
我可以使用這樣的羅馬數字按順序編號一首詩的任何節:
Injurious love, why still to mar accord Between desires has been thy favourite feat? Why does it please thee so, perfidious lord, Two hearts should with a different measure beat? Thou wilt not let me take the certain ford, Dragging me where the stream is deep and fleet. Her I abandon who my love desires, While she who hates, respect and love inspires. Thou to Rinaldo show'st the damsel fair, While he seems hideous to that gentle dame; And he, who when the lady's pride and care, Paid back with deepest hate her amorous flame, Now pines, himself, the victim of despair, Scorned in his turn, and his reward the same. By the changed damsel in such sort abhorred, She would choose death before that hated lord. He to the Pagan cries: "Forego thy theft, And down, false felon, from that pilfer'd steed; I am not wont to let my own be reft. And he who seeks it dearly pays the deed. More -- I shall take from thee yon lovely weft; To leave thee such a prize were foul misdeed; And horse and maid, whose worth outstrips belief, Were ill, methinks, relinquished to a thief." "Thou liest," the haughty Saracen retorts, As proud, and burning with as fierce a flame, "A thief thyself, if Fame the truth reports: But let good deeds decide our dubious claim, With whom the steed or damsel fair assorts: Best proved by valiant deeds: though, for the dame, That nothing is so precious, I with thee (Search the wide world throughout) may well agree."
進入這個:羅馬大寫數字與最後一節對齊?
Injurious love, why still to mar accord Between desires has been thy favourite feat? Why does it please thee so, perfidious lord, Two hearts should with a different measure beat? Thou wilt not let me take the certain ford, Dragging me where the stream is deep and fleet. Her I abandon who my love desires, I While she who hates, respect and love inspires. Thou to Rinaldo show'st the damsel fair, While he seems hideous to that gentle dame; And he, who when the lady's pride and care, Paid back with deepest hate her amorous flame, Now pines, himself, the victim of despair, Scorned in his turn, and his reward the same. By the changed damsel in such sort abhorred, II She would choose death before that hated lord. He to the Pagan cries: "Forego thy theft, And down, false felon, from that pilfer'd steed; I am not wont to let my own be reft. And he who seeks it dearly pays the deed. More -- I shall take from thee yon lovely weft; To leave thee such a prize were foul misdeed; And horse and maid, whose worth outstrips belief, III Were ill, methinks, relinquished to a thief." "Thou liest," the haughty Saracen retorts, As proud, and burning with as fierce a flame, "A thief thyself, if Fame the truth reports: But let good deeds decide our dubious claim, With whom the steed or damsel fair assorts: Best proved by valiant deeds: though, for the dame, That nothing is so precious, I with thee IV (Search the wide world throughout) may well agree."
我想知道是否有任何方法可以使用文本實用程序在 linux 中進行此操作。它可能不適合 awk,以羅馬風格生成數字,但我已經在網際網路上的某個地方找到了一個 bash 腳本來生成羅馬數字
#/bin/bash # roman.sh # # Function # num2roman() { # NUM # Returns NUM in roman letters # input=$1 # input num output="" # Clear output string len=${#input} # Initial length to count down roman_val() { # NUM one five ten # This sub does the basic 'roman' algorythm # N=$1 one=$2 five=$3 ten=$4 out="" case $N in 0) out+="" ;; [123]) while [[ $N -gt 0 ]] do out+="$one" N=$(($N-1)) done ;; 4) out+="$one$five" ;; 5) out+="$five" ;; [678]) out+="$five" N=$(($N-5)) while [[ $N -gt 0 ]] do out+="$one" N=$(($N-1)) done ;; 9) while [[ $N -lt 10 ]] do out+="$one" N=$(($N+1)) done out+="$ten" ;; esac echo $out } while [[ $len -gt 0 ]] do # There are letters to add num=${input:0:1} # Do action according position case $len in 1) # 1 output+="$(roman_val $num I V X)" ;; 2) # 10 output+="$(roman_val $num X L C)" ;; 3) # 100 output+="$(roman_val $num C D M)" ;; *) # 1000+ # 10'000 gets a line above, 100'000 gets a line on the left.. how to? num=${input:0:(-3)} while [[ $num -gt 0 ]] do output+="M" num=$(($num-1)) done ;; esac input=${input:1} ; len=${#input} done echo $output } # # Call it # num2roman $1
我用這種語法呼叫:
for N in `seq 1 10`;do ./roman.sh $N; done
誰的輸出是:
I II III IV V VI VII VIII IX X
因此,從另一個角度來看,這可能只是從生成的羅馬數字列表中挑選任何項目,然後將其中任何一項與任何一節的最後一節對齊
在每個塊之後的新行上列印記錄號可能更簡單。如果您的空白行完全空白(即它們是
\n\n
而不是\n \n
),則可以使用 AWK 的“段落模式”(空字元串為RS
):function d2r(n, m) { m = sprintf("%*s", int(n/1000), "") gsub(/ /, "M", m) return m r100[int(n%1000/100)] r10[int(n%100/10)] r1[int(n%10)] } BEGIN { split("C,CC,CCC,CD,D,DC,DCC,DCCC,CM", r100, ",") split("X,XX,XXX,XL,L,LX,LXX,LXXX,XC", r10, ",") split("I,II,III,IV,V,VI,VII,VIII,IX", r1, ",") RS = "" } { print print d2r(NR) " (" NR ")" print "" }
以上另存為
roman_numeral_blocks.awk
$ printf '%s\n\n' foo bar | awk -f roman_numeral_blocks.awk foo I (1) bar II (2)
該
" (" NR ")"
部分是為了讓您可以驗證d2r()
產生了正確的結果。d2r()
嘗試為給定的十進制數生成羅馬數字。每 1000 手都簡單地重複“M”。如果您希望將數字列印在與塊的最後一行相同的行上,那麼您必須弄清楚如何保留原始縮進,以及當頁邊距中的可用空間小於所需空間時該怎麼辦列印號碼。
要將上述內容應用於 OPs 範例,使用任何 POSIX awk,將是:
$ cat roman_numeral_blocks.awk function d2r(n, m) { m = sprintf("%*s", int(n/1000), "") gsub(/ /, "M", m) return m r100[int(n%1000/100)] r10[int(n%100/10)] r1[int(n%10)] } BEGIN { split("C,CC,CCC,CD,D,DC,DCC,DCCC,CM", r100, ",") split("X,XX,XXX,XL,L,LX,LXX,LXXX,XC", r10, ",") split("I,II,III,IV,V,VI,VII,VIII,IX", r1, ",") } NR > 1 { prt(0) } { prev = $0 } END { prt(1) } function prt(isEnd, pfx) { if ( (!NF || isEnd) && (match(prev, /[^[:space:]]/)) ) { prev = substr(prev, RSTART) pfx = sprintf("%-*s", RSTART-1, d2r(++numParas) " ") } print pfx prev }
$ awk -f roman_numeral_blocks.awk file Injurious love, why still to mar accord Between desires has been thy favourite feat? Why does it please thee so, perfidious lord, Two hearts should with a different measure beat? Thou wilt not let me take the certain ford, Dragging me where the stream is deep and fleet. Her I abandon who my love desires, I While she who hates, respect and love inspires. Thou to Rinaldo show'st the damsel fair, While he seems hideous to that gentle dame; And he, who when the lady's pride and care, Paid back with deepest hate her amorous flame, Now pines, himself, the victim of despair, Scorned in his turn, and his reward the same. By the changed damsel in such sort abhorred, II She would choose death before that hated lord. He to the Pagan cries: "Forego thy theft, And down, false felon, from that pilfer'd steed; I am not wont to let my own be reft. And he who seeks it dearly pays the deed. More -- I shall take from thee yon lovely weft; To leave thee such a prize were foul misdeed; And horse and maid, whose worth outstrips belief, III Were ill, methinks, relinquished to a thief." "Thou liest," the haughty Saracen retorts, As proud, and burning with as fierce a flame, "A thief thyself, if Fame the truth reports: But let good deeds decide our dubious claim, With whom the steed or damsel fair assorts: Best proved by valiant deeds: though, for the dame, That nothing is so precious, I with thee IV (Search the wide world throughout) may well agree."