Solved

Grep & Regular Expressions

Posted on 1998-11-13
14
354 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:MikeBridge
14 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2007824
why don't you want to use grep -v '^t9'?
have you tried:
grep '^[^tz]\|^.[^9]' datafile > output.temp
0
 

Author Comment

by:MikeBridge
ID: 2007825
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....
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 2007826
I believe that you could use the '-e' opt
0
 
LVL 3

Expert Comment

by:braveheart
ID: 2007827
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.
0
 

Author Comment

by:MikeBridge
ID: 2007828
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.
0
 
LVL 1

Expert Comment

by:gabsi
ID: 2007829
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.
0
 
LVL 1

Expert Comment

by:gabsi
ID: 2007830
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.

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:gabsi
ID: 2007831
Sorry I forgot an ^ after the | ;
egrep ^"[^t^z]|^[tz][^9]"
0
 

Author Comment

by:MikeBridge
ID: 2007832
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.
0
 

Author Comment

by:MikeBridge
ID: 2007833
Edited text of question
0
 
LVL 2

Expert Comment

by:cwalter
ID: 2007834
grep -v t9 | grep -v z9
0
 

Author Comment

by:MikeBridge
ID: 2007835
you need to do it WITHOUT using any options. trust me guys, this is possible.
0
 
LVL 2

Accepted Solution

by:
blowfish earned 100 total points
ID: 2007836
What's wrong with this ???

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

--frankf
0
 

Author Comment

by:MikeBridge
ID: 2007837
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now