在bash脚本中,除问号和感叹号外,使用perl将句点替换为替代



最初我想出了如何删除所有文件的句号,然后再添加它们:

 Remove periods at end of titles
 perl -pi -e 's/title = {(.*).},/title = {$1},/g' $1
 # Add periods back so all files are the same (comment out if no periods wanted)
 perl -pi -e 's/title = {(.*)},/title = {$1.},/g' $1

理想情况下,我想做的是检查每个标题是否有句号,感叹号或问号,如果没有,然后添加句号。我认为有一种简单的方法来做这个替换,但我不太了解语法。

例如输入:

title = This has a period.
title = This has nothing
title = This has a exclamation!
title = This has a question?

输出将是:

title = This has a period.
title = This has nothing.
title = This has a exclamation!
title = This has a question?

所以它只会在没有任何标记的情况下才会将行修改为句号

KISS,使用否定字符类

perl -pi -e 's/title = {(.*[^.?!])},/title = {$1.},/g' $1

演示

使用消极的目光。

perl -pi -e 's/title = {(.*)(?<![.?!])},/title = {$1.},/g' $1
演示

您可以使用此sed:

sed '/^title = .*[.!?]$/!s/$/./' file

(或)

sed '/^title = .*[^.!?]$/s/$/./' file

测试:

$ sed '/^title = .*[.!?]$/!s/$/./' file
title = This has a period.
title = This has nothing.
title = This has a exclamation!
title = This has a question?

最新更新