fork download
  1. #!/bin/bash
  2. # ideone boilerplate - we can't write files in the home directory;
  3. # so create a temporary directory for our files instead
  4. t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
  5. trap 'rm -rf "$t"' ERR EXIT
  6. cd "$t"
  7.  
  8. cat <<\: >filea.csv
  9. Foo,Bar,Baz,Quux,There is #Corona and #Covid here,False positive?
  10. Foo,Bar,Baz,Quux,There is Corona and #Covid here,This should be matched
  11. Foo,Bar,Baz,Quux,There is #Corona and Covid here,This should be matched
  12. Foo,Bar,Baz,Quux,There is #Corona and #Covid here,False positive?
  13. Foo,Bar,Baz,Quux,"Quoted comma, There is Corona and Covid here",False negative for non-Python/grep -P cases
  14. Corona,Covid,Baz,Quux,There is #Corona and #Covid here,False positive for grep -P case
  15. :
  16.  
  17.  
  18. cat <<\: >prog.py
  19. #!/usr/bin/env python3
  20.  
  21. import csv
  22. import re
  23. import sys
  24.  
  25. reader = csv.reader(sys.stdin)
  26. writer = csv.writer(sys.stdout)
  27. for line in reader:
  28. if re.search(r'(?<!#)\b(?:Corona|Covid)\b', line[4], re.IGNORECASE):
  29. writer.writerow(line)
  30. :
  31.  
  32.  
  33. echo '** grep -P **'
  34. grep -Pwi '(?<!#)(Corona|Covid)' filea.csv
  35.  
  36. echo
  37. echo '** grep -E **'
  38. grep -Ei '^([^,]*,){4}(#?[^,#]+)*\b(Corona|Covid)\b' filea.csv
  39.  
  40. echo
  41. echo '** awk **'
  42. awk -F, -v col=5 '{ field = tolower($col); gsub(/#[A-Za-z0-9_]+/, "", field) }
  43. field ~ /\<(corona|covid)\>/' filea.csv
  44.  
  45.  
  46. echo
  47. echo '** python3 **'
  48. python3 prog.py <filea.csv
  49.  
Success #stdin #stdout 0.04s 9672KB
stdin
Standard input is empty
stdout
** grep -P **
Foo,Bar,Baz,Quux,There is Corona and #Covid here,This should be matched
Foo,Bar,Baz,Quux,There is #Corona and Covid here,This should be matched
Foo,Bar,Baz,Quux,"Quoted comma, There is Corona and Covid here",False negative for non-Python/grep -P cases
Corona,Covid,Baz,Quux,There is #Corona and #Covid here,False positive for grep -P case

** grep -E **
Foo,Bar,Baz,Quux,There is Corona and #Covid here,This should be matched
Foo,Bar,Baz,Quux,There is #Corona and Covid here,This should be matched

** awk **
Foo,Bar,Baz,Quux,There is Corona and #Covid here,This should be matched
Foo,Bar,Baz,Quux,There is #Corona and Covid here,This should be matched

** python3 **
Foo,Bar,Baz,Quux,There is Corona and #Covid here,This should be matched
Foo,Bar,Baz,Quux,There is #Corona and Covid here,This should be matched
Foo,Bar,Baz,Quux,"Quoted comma, There is Corona and Covid here",False negative for non-Python/grep -P cases