match works, don't match doesn't 
Author Message
 match works, don't match doesn't

@cts.com
Organization: CTS Network Services
Mime-Version: 1.0

Newsgroups: comp.lang.awk

The following is supposed to search an input file and produce 2 output
files. The input file consists of text lines of two fields, delimited
by whitespace. The first output file is to contain those lines in the
input file where any one of the variable strings match the first
field. (This works.) The second output file is to contain those lines
where the variable strings do not match, i.e. all the lines in the
input file except those found with the first search. (This doesn't
work.)

###command-line parameter placed in variable.###


   searchdot="\.$1"
   searchexact=$1

awk '{IGNORECASE=1} $1 ~ /'^"$searchexact"$'/ || /'"$searchat"'/ ||
/'"$searchdot"'/ { print $0 }' input.file > output1.file

awk '{IGNORECASE=1} $1 !~ /'^"$searchexact"$'/ || /'"$searchat"'/ ||
/'"$searchdot"'/ { print $0 }' input.file > output2.file

Any help gratefully accepted.  



Wed, 05 Jul 2000 03:00:00 GMT  
 match works, don't match doesn't


Quote:

>The following is supposed to search an input file and produce 2 output
>files. The input file consists of text lines of two fields, delimited
>by whitespace. The first output file is to contain those lines in the
>input file where any one of the variable strings match the first
>field. (This works.) The second output file is to contain those lines
>where the variable strings do not match, i.e. all the lines in the
>input file except those found with the first search. (This doesn't
>work.)

>###command-line parameter placed in variable.###


>   searchdot="\.$1"
>   searchexact=$1

>awk '{IGNORECASE=1} $1 ~ /'^"$searchexact"$'/ || /'"$searchat"'/ ||
>/'"$searchdot"'/ { print $0 }' input.file > output1.file

>awk '{IGNORECASE=1} $1 !~ /'^"$searchexact"$'/ || /'"$searchat"'/ ||
>/'"$searchdot"'/ { print $0 }' input.file > output2.file

>Any help gratefully accepted.  

This is not the normal way to pass shell variables to awk.  See man awk

Bassically you'd do something like:

awk '$1 ~ /var1/ ' var1=$searchexact input.file > output1.file

which is a simplified version of what you had to give you an idea.

Put the IGNORECASE thing in a BEGIN section, and if you only want to
print the entire line the { print $0 } is unnecessary.

Are you sure that you have the right logic for what you're trying to
do in the second case?  I'd try adding parentheses to insure that
things are grouped properly to get the proper precedence of operators.

Just some quick thoughts, I haven't tried any of it.

BTW, you could do this with egrep using the -i and -v options as
appropriate, I believe.  Egrep allows the OR operator "|".

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 05 Jul 2000 03:00:00 GMT  
 match works, don't match doesn't



%    searchdot="\.$1"
%    searchexact=$1

% awk '{IGNORECASE=1} $1 ~ /'^"$searchexact"$'/ || /'"$searchat"'/ ||
% /'"$searchdot"'/ { print $0 }' input.file > output1.file

I would do something like
 awk 'BEGIN {IGNORECASE=1}

to reverse this, you need to change both the comparison operators and the
conjunctions.
 awk 'BEGIN {IGNORECASE=1}

You could put this into one pass with arguably simplified patterns like this:

 awk 'BEGIN {IGNORECASE=1;
             pat[ENVIRON["searchexact"]] = 1
             pat[ENVIRON["searchat"]] = 1
             pat[ENVIRON["searchdot"]] = 1 }
      $1 in pat { print > "output1.file" }
      !($1 in pat) { print > "output2.file" }'

although I don't know if IGNORECASE will work with the `in' operator.
--

Patrick TJ McPhee
East York  Canada



Wed, 05 Jul 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. match doesn't work in awk

2. Source and sink don't match ???

3. debug and patch don't match adresses

4. Error: operands given don't match any known 386 instruction

5. Tk system fonts don't match actual system fonts

6. VO windows editor font doesn't match

7. \s doesn't match

8. regsub doesn't seem to match largest pattern

9. regexp: matching word that doesn't being with specific text

10. BLT installs but doesn't work, and TkTable doesn't build

11. 'No font matching ...' problem

12. WHat is 'Matching Else Problem'

 

 
Powered by phpBB® Forum Software