1

I created one table of employees.

    1 Andy Account
    2 Grecie HR
    3 Jyorge Marketing
    4 Seeya HR
    5 Princy Account
    6 Siya Production

Here the names of 4th employee and 6th employee are same but spellings are different. So i want to display only that records using grep command. I tried it like:

grep S[iee]ya emp

and

grep S[[i][ee]]ya emp

but it didn't work. Any solution?

  • 1
    you can also use grep -e 'Seeya' -e 'Siya' ... helps if you want to add -F option as well – Sundeep Jul 18 '17 at 12:23
  • You might also want to use the -w (whole word) option for this command. – Toby Speight Jul 18 '17 at 17:28
  • to do this properly, you probably should use a language like perl or python that has a library implementing the Soundex or similar algorithm. – cas Jul 19 '17 at 1:24
9

You need the OR operator '|' in grep:

grep -E 'S(i|ee)ya' emp

4 Seeya HR
6 Siya Production
9

[...] creates a character class, not a group, you want to use a group that contains an | or operator

grep 'S\(ee\|i\)ya' emp

will do such a group and find those two lines. You can also clean that up a bit by telling grep to use extended regex with -E, then you don't have to escape the characters to make them special

grep -E 'S(ee|i)ya' emp
-2
grep 'S.*ya' 

seems to work also. The * quantifier matches between zero and unlimited times.

  • 1
    Would this not possibly match some unwanted things? – Stephen Rauch Jul 18 '17 at 18:02
  • 2
    Would also return true if the name is 'Shit ya' – GypsyCosmonaut Jul 18 '17 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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