0

Originally I figured out how to remove the periods for all the files and then add them back:

 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

Ideally what I want to do is check if every title has a period, exclamation mark, or question mark and if it doesn't then add a period. I assume there is a simple way to do this substitution but I don't know the syntax well.

So for example for input:

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

The output will be:

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

So it only modifies lines to had a period if it ended without any markings.

J Spen
  • 2,614
  • 4
  • 26
  • 41

2 Answers2

3

KISS, Use negated character class.

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

DEMO

or

Use negative lookbehind.

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

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can use this sed:

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

(OR)

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

Test:

$ sed '/^title = .*[.!?]$/!s/$/./' file
title = This has a period.
title = This has nothing.
title = This has a exclamation!
title = This has a question?
sat
  • 14,589
  • 7
  • 46
  • 65