0

I am new to Shell scripting. I am trying to write a code that should grep few lines from a huge file based on certain condition.

Contents of file, say names.txt

1 ae1aee2sonata om,vadodara,23-Aug-2016
2 chdc501ae om,patna,26-Aug-2016
3 chdc4326aee6 om,bhuvi,01-Oct-2016
4 ae3aee6prsons hqr,bangalore,29-Aug-2016
5 praaeei5 om,lucknow,11-Nov-2016
6 aetaeen6pana om,phanto,13-Oct-2016

and goes on for 500 or more entries.

Now, I am looking for output for the following :

  1. Filter lines with only "aee" available in it. So, the output will look like:
3 chdc4326aee6.om,bhuvi,01-Oct-2016
5 praaeei5 om,lucknow,11-Nov-2016
  1. Filter lines with only "ae" and "ae + "aee" available in the file. So, the output will look like:
1 ae1aee2sonata.hqr,vadodara,23-Aug-2016
2 chdc501ae.om,patna,26-Aug-2016
4 ae3aee6prsons hqr,bangalore,29-Aug-2016
6 aetaeen6pana om,phanto,13-Oct-2016
  1. Filter lines with only "ae" from the file. So, the output will look like:
2 chdc501ae.om,patna,26-Aug-2016

Any suggestions please. You can point to a good place for getting more information about this, so I can learn.

Toto
  • 89,455
  • 62
  • 89
  • 125
Ram
  • 37
  • 4
  • This would be a good example on how to use regular expression on shell scripts http://stackoverflow.com/questions/19737675/shell-script-how-to-extract-string-using-regular-expressions – saurabh baid Nov 11 '16 at 11:05
  • Hi Tim, I trying with basic Shell scripting with bash shell – Ram Nov 11 '16 at 11:06
  • Similarly if you want to learn on regular expression, just google it and you will so may places to start with – saurabh baid Nov 11 '16 at 11:06
  • You are going to need to use negative lookaheads to achieve what you want. Can you try the following regex and let us know if it works: `^((?!aee).)*ae((?!aee).)*$` – Tim Biegeleisen Nov 11 '16 at 11:33
  • command: `cat names.txt |grep '^((?!aee).)*ae((?!aee).)*$'` gives no output. – Ram Nov 11 '16 at 11:42
  • @Ram, does this regex yield good results: `.*ae[^e].*(aee)?.*` ? This is for lines that have ae and (ae + aee) – Lupu Silviu Nov 11 '16 at 12:13

1 Answers1

1

Use grep with option -P and lookahead

The file:

$ cat data.txt 
1 ae1aee2sonata om,vadodara,23-Aug-2016
2 chdc501ae om,patna,26-Aug-2016
3 chdc4326aee6 om,bhuvi,01-Oct-2016
4 ae3aee6prsons hqr,bangalore,29-Aug-2016
5 praaeei5 om,lucknow,11-Nov-2016
6 aetaeen6pana om,phanto,13-Oct-2016

Find aee but not ae :

$ grep -P '^(?:(?=.*aee[^e]))?(?!.*ae[^e]).*(aee)[^e]' data.txt 
3 chdc4326aee6 om,bhuvi,01-Oct-2016
5 praaeei5 om,lucknow,11-Nov-2016

Find ae or ae + aee :

$ grep -P '^(?:(?!.*aee[^e]))?(?=.*ae[^e]).*(aee?)[^e]' data.txt
1 ae1aee2sonata om,vadodara,23-Aug-2016
2 chdc501ae om,patna,26-Aug-2016
4 ae3aee6prsons hqr,bangalore,29-Aug-2016
6 aetaeen6pana om,phanto,13-Oct-2016

Find ae only :

$ grep -P '^(?!.*aee[^e])(?=.*ae[^e]).*(ae)[^e]' data.txt
2 chdc501ae om,patna,26-Aug-2016
Toto
  • 89,455
  • 62
  • 89
  • 125
  • @Ram: You're welcome, glad it helps. Feel free to mark the answer as accepted if it works for you. – Toto Nov 12 '16 at 12:40
  • **Thank You** Mr. Toto...To openly say, I really don't understand the statements. This really works out my problem...Is there a place for me to get to know more stuffs like this...I googled and googled..still googling.... – Ram Nov 12 '16 at 12:44
  • @Ram: You will find a regex tutorial here: http://www.regular-expressions.info/ For `grep -P` just do `man grep` – Toto Nov 12 '16 at 12:48