grep and regular expressions :


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep and regular expressions :
# 1  
Old 08-14-2006
grep and regular expressions :

I wrote a simple korn shell where I am trying to filter all the good record layouts of a file to only leave the bad ones to look at. That file is hudge. Aside from '# comments' and 'var=ssss', all record should follow a specific record layout, with comma seperated fields. Some fields can have any value, others a choice of 4-5 different values.

First I tried running the script but noticed at the end that some grep didn't filter out some cases. So I tried them one by one until i got to the grep4 creation. It doesn't filter out the case it looks for.

grep -v -i '^#' filea.txt >grep1.tmp
grep -v -i "=" grep1.tmp >grep2.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,infrastructure,slow,__NONE__,__NONE__" grep2.tmp >grep3.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,infrastructure,fast,__NONE__,__NONE__" grep3.tmp >grep4.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,infrastructure,hybrid,__NONE__,__NONE__" grep4.tmp >grep5.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,??,fast,__NONE__,__NONE__" grep5.tmp >grep6.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,??,slow,__NONE__,__NONE__" grep6.tmp >grep7.tmp
grep -v -i "^*.aaa.bbb.ccc,*,??,??,hybrid,__NONE__,__NONE__" grep7.tmp >grep8.tmp
cat grep8.tmp | more
rm grep?.tmp



grep3.tmp file
...
usbisbb007zfs36.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
uslaqlr007zfs36.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
uslaqlr017zfs36.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
ussvsbb017zfs36.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__

>grep -v -i "^*.aaa.bbb.ccc,*,??,infrastructure,fast,__NONE__,__NONE__" grep3.tmp | more
uslastm007zfsx6.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
uslasmn007zfsx6.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
uslassh007zfs36.aaa.bbb.ccc,uslastm007zfsx6-zfs-gaw,us,infrastructure,fast,__NONE__,__NONE__
...

Are some of my grep characters special reserved ones ?
Why doesn't it work ?
# 2  
Old 08-15-2006
You miss quite basic concepts of regex, try to browse thru these forums and FAQs and learn regex, for your current problem, try something like this:
Code:
grep -v -i "^[^\.]*\.aaa\.bbb\.ccc,[^,]*,us,infrastructure,fast,__NONE__,__NONE__$" grep3.tmp

Explanation of above code:
Code:
^[^\.]*\.       Parse from the begining of the line until it finds first "." dot.
\.              A "\" is being used here to escape dot's sepcieal feature in regex.
aaa\.bbb\.ccc,  Search for aaa.bbb.ccc,
[^,]*,          Parse until it finds next "," comma
                And rest is simple string until end of line "$"

Regards,
Tayyab
# 3  
Old 08-15-2006
grep -v -i "^[^.]*\.aaa\.bbb\.ccc,[^,]*,[^,][^,],infrastructure,fast,__NONE__,__NONE__"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep and regular expressions

Hi All, For the past many days I have solved a lot of grep and regular expression questions, Now I am in a search for a good quality set of questions that can help me build and check my knowledge of grep with regular expressions, it would be great if anyone could help me with my requirement. ... (1 Reply)
Discussion started by: rahulkalra9
1 Replies

2. Homework & Coursework Questions

Regular Expressions with GREP

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Given a text file (big_english.txt) containing roughly 250,000 words, answer the following using grep and... (2 Replies)
Discussion started by: blahblahblah123
2 Replies

3. UNIX for Dummies Questions & Answers

extract columns using grep or regular expressions

I am trying to print columns from a table whose name (header) matches a certain string. E.g., patient1001 patient1002 patient2005 patient3005 patient4001 0 0 0 0 0 2 9 2 8 3 2 7 3 0 2 Say I want to print columns whose names end with "01" patient1001 patient4001 0 0 2 3 2 2 ... (3 Replies)
Discussion started by: quextil
3 Replies

4. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

5. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

6. Shell Programming and Scripting

How to grep using a line break in regular expressions?

Hi, I have a file as below, {#### if file then file else file } print file i need to fine the count of all the pattern - file, inside the { } i'm using a grep command as grep -c \{'*file*'\} fake.sh\ It doesn't gives me any result, i think the problem here is the... (5 Replies)
Discussion started by: divak
5 Replies

7. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

8. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

9. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

10. UNIX for Dummies Questions & Answers

More Grep - Regular Expressions

Hey all! I'm trying to search a file and return all instances of a word, let's say 'foo' in this case, as long as it's not a function name. For example: 1) int foo; //OK 2) //'this is totally fooed up' is also OK 3) int foo (int x, int y) //not ok to return I've tried a lot of regular... (7 Replies)
Discussion started by: Jombee
7 Replies
Login or Register to Ask a Question