Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 10 down vote accepted

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
share|improve this answer
+1 thanks. Is there any way without using -v switch ? – Hamed Kamrava Jul 3 '14 at 13:50
@HamedKamrava Yes. Please see my edit. – Joseph R. Jul 3 '14 at 13:51
@HamedKamrava Out of curiosity: why would you want to avoid -v? – Bernhard Jul 3 '14 at 13:54
I don't avoid -v just want to know more :) – Hamed Kamrava Jul 3 '14 at 13:58
@HamedKamrava Please don't forget to mark this answer 'Accepted' if it solved your problem. – Joseph R. Jul 3 '14 at 13:59

In sed you can do something like:

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

In awk you can do:

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

share|improve this answer

A Perl ones:

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

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


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 ¢.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.