Vishnu J
Vishnu J

Reputation: 89

Match a string using grep

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

Answers (3)

Anthony Geoghegan
Anthony Geoghegan

Reputation: 11983

The following command should return the lines you want:

grep -v '^[0-9a-zA-Z ]*$' test.txt

Explanation

  • [0-9a-zA-Z ] matches a space or any alphanumeric character.
  • Adding the asterisk matches any string containing only these characters.
  • Prepending the pattern with ^ 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.
  • Finally, the -v or --invert-match option to grep inverts the sense of matching, i.e., select non-matching lines.

Upvotes: 2

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

I would use:

grep [^0-9a-zA-Z\s]+ test.txt

live example

Or, even better:

 grep -i "[^\da-z\s]" test.txt

Upvotes: 0

miken32
miken32

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

Related Questions