Solved

Grep & Regular Expressions

Posted on 1998-11-13
14
359 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
removing nim resources 5 47
Java core in Solaris 10 1 262
Access Required to Read Linux Security Settings and User Permissions 13 112
Unix/bash: scripted arithmetic 13 92
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

947 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

20 Experts available now in Live!

Get 1:1 Help Now