Solved

Grep & Regular Expressions

Posted on 1998-11-13
14
363 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.:
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.

786 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