Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

Grep & Regular Expressions

here's the problem:
parse a file with grep and find all the lines that do not
start with z9 or t9 without using any options for grep (and no egrep)
i tried:
grep '^[^t][^9]' datafile | grep '^[^z][^9]' > output.temp
but that will kill some of the lines i need (z1's and t3's)
any ideas? remember, no grep options and no egrep.
more than one grep statement will most likely be used.
1 Solution
why don't you want to use grep -v '^t9'?
have you tried:
grep '^[^tz]\|^.[^9]' datafile > output.temp
MikeBridgeAuthor Commented:
i need to do it with out using any options. if i could use options, i would just do
grep -v '^[tz]9' datafile > output.temp
what's the escaped pipe for? does grep have an 'or' function? i thought egrep added that....
I believe that you could use the '-e' opt
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

That 'escaped pipe' is an OR function and it may not need escaping, depending on the shell you are using and any globbing settings. You still haven't said why you can't use options or egrep, or is this some esoteric homework problem that has been set.
MikeBridgeAuthor Commented:
Yes, it was a homework question, but i thought that it was an interesting homework problem, so i posted it here.  It is possible to do without options (takes more than one grep statement though.) on a side note, the pipe should technically only work in egrep (egrep uses extended regular expressions) as an OR.
grep '^[^t][^9]' datafile | grep '^[^z][^9]'
means that you look for all what does not have t in first position AND does not have 9 in 2nd.
"t9abcd" AND "A9xyz" contain 9 in 2nd position and are discarded.
All lines begining by t are discarded.
The second grep make the same for lines begining by z.
egrep ^"[^t^z]|[tz][^9]"

seems to work well:
you need lines which not begin by t nor z OR the lines beginng
by[tz]and does not have 9 in second position.

Sorry I forgot an ^ after the | ;
egrep ^"[^t^z]|^[tz][^9]"
MikeBridgeAuthor Commented:
grep '^[^t][^9]' datafile | grep '^[^z][^9]' will not work (as soon as grep finds a line that starts with a 't' for the first part of the statement, the line is discarded. and as soon as the second part of the statement finds a line that starts with a z the line will be discarded.
egrep "^[^t^z]|^[tz][^9]" will most likely work, but the question asks for the use of the grep statement.
MikeBridgeAuthor Commented:
Edited text of question
grep -v t9 | grep -v z9
MikeBridgeAuthor Commented:
you need to do it WITHOUT using any options. trust me guys, this is possible.
What's wrong with this ???

  $ grep '^.[^9]' data > results
  $ grep '^.[9]' data | grep '^[^tz]' >> results

MikeBridgeAuthor Commented:
That should work. The way i solved it is:
grep '^[^tz]' data > output; grep '^[tz][^9]' data >> output
this uses one less grep, but with the speed of today's system, the difference wouldn't be noticeable.
the idea behind it is a boolean intersection, kill all the lines that start with a t or z, then go back through, grab those lines that do begin with a t or z, but kill the ones that the t or z is followed by a 9 and append this set to the output.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now