Print all lines which not containing punctuation

I want a regex pattern which can print all lines which not containing punctuation :

Input :

.This is line 1
This is ! line 2
This is line (3)
This is line 4

Output: ( should be)

This is line 4

What I’ve tried so far :

grep '[^[:punct:]]' file.txt

But it shows all characters which is not punctuation.

Asked By: Hamed Kamrava

||

Your grep prints all lines containing non-punctuation characters. That’s not the same as printing all lines that do not contain punctuation characters.

For the latter, you want the -v switch (print lines that don’t match the pattern):

grep -v '[[:punct:]]' file.txt

If, for some reason you don’t want to use the -v switch, you must make sure that the whole line consists of non-punctuation characters:

grep '^[^[:punct:]]+$' file.txt
Answered By: Joseph R.

In sed you can do something like:

sed '/[[:punct:]]/!d'

In awk you can do:

awk '!/[[:punct:]]/'

Answered By: ryekayo

A Perl ones:

perl -nle 'print unless /p{XPosixPunct}/' file

This will match -!”#$%&'()*+,./:;<=>?@[]^_`{|}~ which unicode consider Punctutation and Symbols.

or:

perl -nle 'print unless /p{Punct}/' file

p{Punct} only matches -!”#%&'()*,./:;?@[]_{}, missing $+<=>^`|~ which unicode consider Symbols.

perl by default use POSIX locale. If you don’t use perl, you should all so set LC_ALL=POSIX, because different locale can have different punctuation characters like SAA C has ยข.

Answered By: cuonglm
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.