【发布时间】:2017-10-30 00:52:09
【问题描述】:
我需要替换这个:
01:05:01:11 --> 01:05:04:07,so you may continue to support us,|bring us health,
$Italic = True
01:05:04:15 --> 01:05:07:09,well-being,
$Italic = False
01:05:07:21 --> 01:05:13:01,and help us to be one big family|and continue working as a team.
基本上变成这样:
1
01:05:01:11 --> 01:05:04:07,so you may continue to support us,|bring us health,
$Italic = True
2
01:05:04:15 --> 01:05:07:09,well-being,
$Italic = False
3
01:05:07:21 --> 01:05:13:01,and help us to be one big family|and continue working as a team.
EDIT_1:也就是说我需要匹配:
' --> '
并计算它的出现次数。
EDIT_2:因此,例如,我只需要匹配包含以下内容的行:
01:05:04:15 --> 01:05:07:09,
在每一行之前,我需要将上述示例的出现次数插入到文件中。
我想出了这个使用“sed”命令的简短 Shell 脚本,但处理一个更大的文件需要很长时间(例如,超过 60 行)。
# Define the number of the special chars - so you can calculate the number of the subtitle lines
special_chars_no="$(grep -o ' --> ' Output_File | wc -l)"
# Add numbering before every subtitle line
for ((i=1;i<=${special_chars_no};i++)) ;
do
sed -i '/\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\) -->/{:1 ; /\(.*\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\) -->\)\{'"${i}"'\}/!{N;b1} ; s/\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\):\([0-9][0-9]\) -->/'"${i}"'\n\1:\2:\3:\4 -->/'"${i}"' ; :2 ; n ; $!b2}' Output_File
done
我们可以让它可用(更快)吗?
【问题讨论】:
-
抱歉,我需要修改我的输入字符串 - 每行之后还有一个新行。所以当我使用你的代码时 - 输出是:
1 01:05:01:11 --> 01:05:04:07,so you may continue to support us,|bring us health, 2 3 01:05:04:15 --> 01:05:07:09,well-being, 4 5 01:05:07:21 --> 01:05:13:01,and help us to be one big family|and continue working as a team. 6它多放了一个数字 - 这不是所需的输出。 -
请编辑您的帖子并在代码标签中添加示例输入和输出。
-
是的,我这样做了。我对 StackOverflow 有点陌生——我想现在很清楚了。如果不是,请告诉我。谢谢你的帮助,拉文德!
-
您现在应该已经注意到您问题中的示例不适合描述问题。
-
@Xtigyro 添加一些与您的搜索条件不匹配的行会很好,这样示例输入/输出本身就可以很好地了解您的需求......而且,不需要这么大示例中的行,您可以截断它..您尝试的代码尝试匹配数字格式和所有内容,但是您的搜索条件可以简化为仅查找
-->吗?