grep lines having special characters

Hi,

I have a file which has numerous lines and some of the lines having special characters in it. i want to grep the lines which are having special characters.

say,

one line looks like - %*()$#@"", | acbd
antoher line looks like ***##^%! | efcg

so these kind of lines are present in my file.
How to grep these lines. The number of special characters may be differenct from one line to another line and the pattern also not same.

Please suggest.

How about you specify what you don't want to grep for, and find the opposite? grep '[^a-zA-Z0-9 \t]' filename should find any characters which aren't alphanumeric or whitespace.

grep '[[:punct:]]' trtest.txt

---------- Post updated at 03:21 PM ---------- Previous update was at 03:18 PM ----------

grep '[[:punct:]]' <filename>

Thanks guys for reply.

Now i also need to grep a string which has special characters. when i use normal grep it is not working. Please suggest.

say my file looks like,

/u/anaraya8/F200.354/file

$ more file
NULL
!@\#$%^&()+?,
$ echo $VAL
!@\#$%^&
()+?,
$ grep "$VAL" file
$

I am assiging the sting to a variable and try to grep it. it is not returning. Please suggest.

Oh. If you want to grep for an exact string, then:

grep -F '!@#$%' filename
1 Like

thanks. it worked fine...