Skip to main content
added 66 characters in body
Source Link
Ed Morton
  • 36k
  • 6
  • 25
  • 60

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

and we could factor out term1 to have:

awk '/term1/ && (/term2/ || xor(/term3/, /term4/))'

Note that xor() which is a non-standard GNU extension is a bitwise xor, not a logical one. It's fine here as those /regex/ only ever return 0 or 1.

With other awk's you can define a logical xor() as a function:

function xor(a, b) {
  return (a || b && ! (a && b))
}

Or

function xor(a, b) {
  return ((a && !b) || (b && !a))
}

Or

function xor(a, b) {
  return (a ? !b : b)
}

or we could just write the condition without a function:

awk '/term1/ && (/term2/ || (/term3/ ? !/term4/ : /term4/))'

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

Note that xor() which is a non-standard GNU extension is a bitwise xor, not a logical one. It's fine here as those /regex/ only ever return 0 or 1.

With other awk's you can define a logical xor() as a function:

function xor(a, b) {
  return (a || b && ! (a && b))
}

Or

function xor(a, b) {
  return ((a && !b) || (b && !a))
}

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

and we could factor out term1 to have:

awk '/term1/ && (/term2/ || xor(/term3/, /term4/))'

Note that xor() which is a non-standard GNU extension is a bitwise xor, not a logical one. It's fine here as those /regex/ only ever return 0 or 1.

With other awk's you can define a logical xor() as a function:

function xor(a, b) {
  return (a || b && ! (a && b))
}

Or

function xor(a, b) {
  return ((a && !b) || (b && !a))
}

Or

function xor(a, b) {
  return (a ? !b : b)
}

or we could just write the condition without a function:

awk '/term1/ && (/term2/ || (/term3/ ? !/term4/ : /term4/))'
added 371 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

Note that xor() which is a non-standard GNU extension is a bitwise xor, not a logical one. It's fine here as those /regex/ only ever return 0 or 1.

With other awk's you can define a logical xor() as a function:

function xor(a, b) {
  return (a || b && ! (a && b))
}

Or

function xor(a, b) {
  return ((a && !b) || (b && !a))
}

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 

Note that xor() which is a non-standard GNU extension is a bitwise xor, not a logical one. It's fine here as those /regex/ only ever return 0 or 1.

With other awk's you can define a logical xor() as a function:

function xor(a, b) {
  return (a || b && ! (a && b))
}

Or

function xor(a, b) {
  return ((a && !b) || (b && !a))
}
Source Link
muru
  • 78.4k
  • 16
  • 214
  • 320

With awk, as with perl, you'll have to wrap terms in //, but it can be done:

awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))'