Reputation: 89
I want to match the below string using a regular expression in grep command.
File name is test.txt
,
Unknown Unknown
Jessica Patiño
Althea Dubravsky 45622
Monique Outlaw 49473
April Zwearcan 45758
Tania Horne 45467
I want to match the lines containing special characters alone from the above list of lines; the line which I exactly need is 'Jessica Patiño', which contains a non-ASCII character.
I used,
grep '[^0-9a-zA-Z]' test.txt
But it returns all lines.
Upvotes: 0
Views: 654
Reputation: 11983
The following command should return the lines you want:
grep -v '^[0-9a-zA-Z ]*$' test.txt
[0-9a-zA-Z ]
matches a space or any alphanumeric character.^
and appending it with $
anchors the string to the beginning and end of line so that the pattern matches only the lines which contain only the desired characters.-v
or --invert-match
option to grep inverts the sense of matching, i.e., select non-matching lines.Upvotes: 2
Reputation: 29431
I would use:
grep [^0-9a-zA-Z\s]+ test.txt
Or, even better:
grep -i "[^\da-z\s]" test.txt
Upvotes: 0
Reputation: 42682
The provided answers should work for the example text given. However, you're likely to come across people with hyphens or apostrophes in their names, etc. To search for all non-ASCII characters, this should do the trick:
grep -P "[\x00-\x1F\x7F-\xFF]" test.txt
-P enables "Perl" mode and allows use of character code searches. \x00-\x1F are control characters, and \x7F-\xFF is everything above 126.
Upvotes: 1