Vil*_*age 3 ruby python bash perl sed
按包含数字的行排序行,忽略附加到字母的数字
我需要对文件中的行进行排序,使得包含至少一个数字(0-9)的行,在这些字母之一("a","e","g")之前不计数数字1-5 ,"i","n","o","r","u","v"或"u:"(u + :))被移动到文件的末尾.
这是一个示例文件:
I want to buy some food.
I want 3 chickens.
I have no3 basket for the eggs.
I have no3 basket which can hold 24 eggs.
Move the king to A3.
Can you move the king to a6?
Run Code Online (Sandbox Code Playgroud)
在示例文件中,以下是哪些匹配的注释:
I want to buy some food. % does not match
I want 3 chickens. % matches
I have no3 basket for the eggs. % does not match, because "3" is preceded by "o"
I have no3 basket which can hold 24 eggs. % matches, because contains "24"
Move the king to A3. % matches, words preceded by "A" are not ignored.
Can you move the king to a6? % matches, 6 is not 1-5
Run Code Online (Sandbox Code Playgroud)
输出会将所有匹配的行放在底部:
I want to buy some food.
I have no3 basket for the eggs.
I want 3 chickens.
Move the king to A3.
Can you move the king to a6?
I have no3 basket which can hold 24 eggs.
Run Code Online (Sandbox Code Playgroud)
优选地(尽管不是必需的),解决方案将包含最大匹配数字的行分类到末尾.例如"我有10只鸡和12只蝙蝠." 在"我有99只鸡"之后出现(4位数).(2位数).
使用bash解决方案,Perl和Python 2.7版,红宝石,sed,awk,或者grep是罚款.
如果你的grep支持-P(perl-regexp)选项:
pat='(?<=[^0-9]|^)((?<!u:)(?<![aeginoruv])[1-5]|[06-9])'
{ grep -vP "$pat" input.txt; grep -P "$pat" input.txt; } >output.txt
Run Code Online (Sandbox Code Playgroud)
如果您安装了ssed(超级sed):
ssed -nR '
/(?<=[^0-9]|^)((?<!u:)(?<![aeginoruv])[1-5]|[06-9])/{
H
$!d
}
$!p
${
g
s/\n//
p
}' input.txt
Run Code Online (Sandbox Code Playgroud)