?
Solved

Grep & Regular Expressions

Posted on 1998-11-13
14
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

719 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