Match all except string(s) in posix extended


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match all except string(s) in posix extended
# 1  
Old 05-30-2011
Match all except string(s) in posix extended

How to say "match anything but this or that string" using only posix extended syntax? I am not talking about egrep -v, I want this done entirely by the regular expression. Thanks
# 2  
Old 05-30-2011
you does not use regex or exregex for grep -v emulation if you dont want.

Code:
# cat file2
this is a line
test line is ok!!
what is that ?
test2 line is ok!!!
test2 that and this line is NOTok!!!

Code:
# ./justdoit2 this that file2
test line is ok!!
test2 line is ok!!!

Code:
#!/bin/bash
while read -ra l ; do for i in $1 $2 ; do
br=0;br2=0 ; for j in ${l[@]} ; do
if [ $j = $i ] ; then br=1 ; break ; fi ; done
if [ $br -eq 1 ] ; then br2=1 ; break ; fi ; done
if [ $br2 -eq 0 ] ; then echo "${l[@]}" ; fi ; done<$3

regards
ygemici
# 3  
Old 05-30-2011
You really have not given us enough information to provide a useful answer. Do you want to use the extended regular expression with a shell or with a utility like sed?
# 4  
Old 05-30-2011
With find -regex (with -regextype posix extended) not sed or grep. It is for an auditing script that checks for non-standard filenames.

Right now the user only has the ability to specify the place to start looking and the regex itself, in a bunch of files that lay down the rules for the audit. I would like to avoid using '!' since this would greatly alter the way things are done, but it doesn't look like this is possible.

It doesn't look like posix extended syntax is powerful enough to handle this functionality by itself.
# 5  
Old 05-30-2011
This can be done but it's not pretty:

Code:
$ grep -E '^([^t]|t[^h]|th[^ia]|thi[^s]|tha[^t])*.{0,3}$' file2
test line is ok!!
test2 line is ok!!!

And this is a simplified case as the two words ("this" and "that") are the same length and both start with "th".
# 6  
Old 05-31-2011
@Chubler_XL
stevensw does not want grep..
and your grep statement matches with xxxx(this|that) or (that|this)xxxxx in same time.

@stevensw
how do you think compare strings just by use regex ?
is there any input and desired ouptut ?

regards
ygemici
# 7  
Old 05-31-2011
@ygemici
Grep is just used here to demonstrate the extended regular expression in action. The OP has requested we not use the negation options (! or -v), as these add complexity to the system requiring them.

I can't see any problem with the R.E I supplied; can you supply a string that gives a false positive/negative? Remember this R.E. is supposed to match any line that doesn't contain "this" or "that".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match exactly a string

I am formatting my code and for that I am trying to write a script which can quicken some repetitive work. I need to match "==" exactly in a string and replace it by inserting a (single) blank space before and after it. Sample Strings: 1.this.something =='something'.that... (9 Replies)
Discussion started by: prohank
9 Replies

2. Shell Programming and Scripting

Get the exact match of the string!

Hi All, I am breaking my head in trying to get a command that will exactly match my given string. I have searched net and found few of the options - grep -F $string file grep -x $string file grep "^${string}$" file awk '/"${string}"/ {print $0}' file strangely nothing seems to... (3 Replies)
Discussion started by: dips_ag
3 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Match a string but with little difference

I want to output the records matched with a string like "abcdefg", but with one letter difference at any position. Can I do this with awk or grep? Thank you! (1 Reply)
Discussion started by: xshang
1 Replies

5. Shell Programming and Scripting

How can I match the particular character in the string?

Hi, I want to check out a word in the text file and generate a clear report for me to see... The text file content: Content: ............ 20120608: 20120608: ............ 20120608: .......... 2012031201: , hime] End of the file My expected output is: Full TXT: manatsu TXT:... (3 Replies)
Discussion started by: meroko
3 Replies

6. UNIX for Dummies Questions & Answers

How to match 2 columns where one column has data as a range - extended

Dear all, there is a nice solution for a text merge where the second file has only variables with a numeric range ( sorry, cannot post URL + thread is closed ). The real world is however more complicated than in the earlier example. file1 A 1 A 2 A 3 B 1 B 2 B 3 B 4 C 1 C 2 C 3 C... (4 Replies)
Discussion started by: underscore
4 Replies

7. UNIX for Dummies Questions & Answers

How do mask off the string that match my value?

Hi, I have a file like following, aaabb aaavv bbdddaaab fgdgjhaa bfd 12352aa dgs1xaf sdgsdyg4 How can i get the output below(mask off all the line that have "a") by using vim #aaabb #aaavv #bbdddaaab #fgdgjhaa bfd #12352aa (4 Replies)
Discussion started by: 793589
4 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. Shell Programming and Scripting

Perl: Better way to match string within a string

Hi, I'm trying to get one field out of many as follows: A string of multiple fields separated with "/" characters: "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/" I want to pick up the field "ef=pick-this.one" which has no regular pattern except it starts with "ef=xxxx"... (3 Replies)
Discussion started by: Juha
3 Replies

10. UNIX for Dummies Questions & Answers

Posix string to match [[0-999]]

Hi, I would like to set up a Posix string to match the subject line in spam e-mails which contain "" How do I get the square brackets recognised as literals and then include them in a string so that I can reject spam with things like in the subject line. (11 Replies)
Discussion started by: Mair55
11 Replies
Login or Register to Ask a Question