Joe
Joe

Reputation: 15339

How to look for lines which don't end with a certain character

How to look for lines which don't end with a ."

description="This has a full stop."
description="This has a full stop."
description="This line doesn't have a full stop"

Upvotes: 0

Views: 1231

Answers (4)

slebetman
slebetman

Reputation: 113916

In general, regular expression matches. It is not easy to do a don't match. The general solution for this kind of thing is to invert the truth value. For example:

grep: grep -v '\.$'

Perl: $line !~ /\.$/

Tcl: ![regexp {\.$} $line]

In this specific case, since it is just a character, you can use the character class syntax, [], since it accepts a ^ modifier to signify anything that is not the specified characters:

[^.]$

so, in Perl it would be something like:

$line =~ /[^.]$/

Upvotes: 1

Gabriel S.
Gabriel S.

Reputation: 1943

I guess the regular expression pattern you are looking for is the following:

\."$

\. means a real dot. (compared to . which means any character except \n)

" is the double quote that ends the line in your example.

$ means end of line.

The way you will use this pattern depends on the environment you are using, so give us more precision for a more precise answer :-)

Upvotes: 1

Gumbo
Gumbo

Reputation: 655319

You can use a character class to describe the occurrence of any character except .:

[^\n.](\n|$)

This will match any character that is neither a . nor new line character, and that is either followed by a new line character or by the end of the string. If multiline mode is supported, you can also use just $ instead of (\n|$).

Upvotes: 2

thkala
thkala

Reputation: 86353

Depends on your environent. On Linux/Unix/Cygwin you would do something like this:

grep -n -v '\."$' <file.txt

or

grep -n -v '\."[[:space:]]*$' <file.txt

if trailing whitespace is fine.

Upvotes: 2

Related Questions