Sophil
Sophil

Reputation: 223

Output lines that include punctuations in Bash

Given a text file, I want to output lines that include at least one of the following punctuations: ! . ? in Bash.

I've tried doing that for the point . first but so far I haven't succeeded yet.

sed '/^\.*$/d' file
sed '/^\.+$/d' file
sed '/^[.]/d' file

Could you please help me on this problem? Thank you very much in advance for your help!

Upvotes: 0

Views: 152

Answers (1)

KamilCuk
KamilCuk

Reputation: 141583

Remove all lines except those with ! . or ?:

sed '/[!.?]/!d'

or alternatively output only lines that include at least one of those characters:

sed -n '/[!.?]/p'

About your tries:

sed '/^\.*$/d' file

The ^ and $ match the beginning and ending of a line. It removes all lines that have zero or more dots and dots only.

sed '/^\.+$/d' file

Same as above, but one or more. It removes all lines that have one or more dots and only dots.

sed '/^[.]/d' file

This would remove all lines start with a single dot. The [ and ] specify OR relation, so for example [.?] - would be dot or question mark. One character inside [ ] is equal to just that character.

Upvotes: 1

Related Questions