0

I have a situation where I have to use grep to do some string matching, and the option to use the -F switch is not available. The reason it's not available is grep is being invoked from within a 3rd party library over which I have no control.

The string I'm trying to match is exactly

0 2 * * * /usr/bin/find /data/app-logs/ -mindepth 1 -mtime +14 -a \( -name "*.log*" -o -name "*.out*" -o -name "*.err*" \) -delete

, so a crontab entry. The backslashes above are required.

Grep version being used is:

grep (GNU grep) 2.5.1

I've tried escaping all of the [*\.] characters, as I believe that's all that needs escaping with regular grep, but to no avail. Would appreciate any help here. I'm sure it's something small I'm missing.

David Corley
  • 710
  • 5
  • 17
  • 1
    See http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern . The characters needing escaping are: ```sed -e 's/[\/&]/\\&/g'``` – Ramón Gil Moreno Apr 16 '15 at 18:16
  • Do you have the source of the 3rd party library, or can you otherwise relate its invocation of `grep`? Is it done via a shell or system call or via some form of `exec`? – Politank-Z Apr 16 '15 at 18:16
  • Not sure if this is what you want: `grep "^0 2 \* \* \* /usr/bin/find /data/app-logs/ -mindepth 1 -mtime +14 -a \\\( -name \"\*\.log\*\" -o -name \"\*\.out\*\" -o -name \"\*.err\*\" \\\) -delete$" myfile` – Sorawee Porncharoenwase Apr 16 '15 at 18:16
  • @Politank-Z the 3rd party library is serverspec. The specific line where grep is invoked is here: https://github.com/serverspec/specinfra/blob/edf7b1fe1f765e1dbc6c20049f3c60cb46aeb96b/lib/specinfra/command/base/cron.rb#L7 – David Corley Apr 16 '15 at 18:19
  • @tripleee yours doesn't work for me. – Sorawee Porncharoenwase Apr 16 '15 at 18:21
  • @tripleee that still doesn't work for me. I'm not able to use the -n command line param FWIW. – David Corley Apr 16 '15 at 18:22
  • @SoraweePorncharoenwase your solution isn't working for me either. – David Corley Apr 16 '15 at 18:23
  • 1
    The `-n` was just for my own testing (I had a file with many lines of test and wanted to make sure I got exactly the output I wanted). Sorry about that. I see now that I also had a typo there still. The corrected version without `-n` is `grep '0 2 \* \* \* /usr/bin/find /data/app-logs/ -mindepth 1 -mtime +14 -a \\( -name "\*\.log\*" -o -name "\*\.out\*" -o -name "\*\.err\*" \\) -delete' testfile` – tripleee Apr 16 '15 at 18:25
  • Looking at that source, it seems to be trying to do your escaping for you. What language are you making the system call from? – Politank-Z Apr 16 '15 at 18:26
  • 2
    The main question is still "what did you try" and how are you invoking and passing this expression to `grep`. – tripleee Apr 16 '15 at 18:27
  • Have you tried to feed it the unmodified string, or escaping only the backslashes before the `()`? – Politank-Z Apr 16 '15 at 18:34
  • That linked snippet is already escaping three of the characters you said you escaped. That would lead to double escaping if you escaped them also. So the question is again what did you try **exactly**? – Etan Reisner Apr 16 '15 at 18:49
  • @tripleee, actually your corrected solution did work (at least running directly from the command line). Thanks! – David Corley Apr 16 '15 at 21:07

1 Answers1

0
grep '0 2 \* \* \* /usr/bin/find /data/app-logs/ -mindepth 1 -mtime +14 -a \\( -name "\*\.log\*" -o -name "\*\.out\*" -o -name "\*\.err\*" \\) -delete' testfile

– tripleee

Armali
  • 18,255
  • 14
  • 57
  • 171
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Martin Oct 06 '15 at 11:44
  • You shouldn't be so trigger-happy with your cut-and-paste comment - there is no link at all. – Armali Oct 06 '15 at 12:00
  • I see your point @Armali , I think perhaps SO should have an auto-comment for "Code only answers", as code without explanation isn't really teaching. Something for the meta...... – Martin Oct 06 '15 at 12:04
  • Not sure if the answer tries to allude to my user id or what. For the record, I don't think I have posted any code which looks even vaguely like this anywhere. (I would certainly use `grep -F` for literal string matching.) – tripleee Dec 02 '15 at 05:35