简体   繁体   中英

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.

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, ie, select non-matching lines.

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.

I would use:

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

live example

Or, even better:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM