我可以使用这样的罗马数字按顺序编号一首诗的任何节:
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
):以上另存为
roman_numeral_blocks.awk
该
" (" NR ")"
部分是为了让您可以验证d2r()
产生了正确的结果。d2r()
尝试为给定的十进制数生成罗马数字。每 1000 手都简单地重复“M”。如果您希望将数字打印在与块的最后一行相同的行上,那么您必须弄清楚如何保留原始缩进,以及当页边空白中的可用空间小于所需空间时该怎么办打印号码。
要将上述内容应用于 OPs 示例,使用任何 POSIX awk,将是:
以下 perl 脚本使用 perl Roman模块。根据您使用的 unix 类型,这可能会为您的操作系统预先打包。例如,在 Debian 和 Ubuntu 或 Mint 等衍生产品上,您可以使用
sudo apt-get install libroman-perl
. 其他 Linux 发行版可能具有类似名称的软件包。否则,安装它cpan
:这以段落模式读取文件(段落边界是一个或多个空行),将每个段落拆分为一个数组 (
@para
),并将除最后一行之外的所有 @para 缩进 8 个空格。@para 的最后一行由一个 6 字符宽的罗马数字缩进,表示节号和另外两个空格。然后打印该数组。对于大写罗马数字,将
uc
函数与函数一起使用roman()
- 即将包含以下内容的第二行替换为sprintf
:如果您希望罗马数字左对齐,请使用
%-6s
. 例如或者
使用Raku(以前称为 Perl_6)
示例输入(直接从上面的 OP 帖子中复制粘贴):
样本输出:
上面的 Raku 代码对预先存在的缩进相当健壮,并且
split
可以调整代码以适应相邻节之间的不同间距,如下所述(如下)。Briefly, The poetry file is
slurp
-ed in andsplit
into@para
paragraphs using the/^^ \h**7 "\n" /
regex, which looks for the pattern 1)^^
start-of-line, 2)\h**7
seven horizontal whitespaces and 3) a\n
newline. [If the lines separating stanzas are truly empty, use/^^ $$ "\n" /
as the regex]. Thesplit
function is instructed to drop empty strings via the parameter/adverb:skip-empty
, leaving 4 paragraphs (i.e stanzas). Paragraphs are numbered withkv
key-value and first passed into afor
loop, then passed into a nestedfor
loop. Within the nestedfor
loop the$v
value (i.e. text) is broken into (8)lines
, and numbered 0-to-7 withkv
(used below for numbering each stanza).Printing is accomplished by
put
, which callssprintf
to format the$k+1
paragraph number as a string, followed by the$l_value
(text) string. Strings are concatenated with~
. Paragraph numbering is accomplished using the three-element, minimum-width (right-justified) string formatter:sprintf( "%*s ", 5, "{to-roman($k+1) if $l_key == 7}")
. Theif
conditional places numbering only on the$l_key == 7
(zero-indexed seventh) line of the paragraph. Since indentation accomplished withsprintf
, each line of the$l_value
poem needs to be trimmed usingtrim-leading
, so that output aligns properly.Finally, the
to-roman(…)
sub converts the$k+1
paragraph counter to Roman Numeral, using theMath::Roman
module called at the command-line with-M
. [In fact, the postfix(…)R
also works here]. Raku modules can be searched at https://modules.raku.org and installed withzef
, see: https://github.com/ugexe/zef .References:
https://modules.raku.org/dist/Math::Roman:cpan:TITSUKI
https://raku.org