Making sure a file path is complete in grep
I had to change the path of my template directory and I want 开发者_运维问答to make sure all my files refer to "templates/app/xxx.html" instead of "templates/xxx.html"
How can I use grep to see all lines of "*.html", but not "app/*.html"?
Assuming there's only one per line, you could start with something like:
grep '\.html' | grep -v '/app/.*\.html'
The first will deliver all those that have .html
. The second will strip from that list all those that have the app
variant, leaving only those that violate your check.
Obviously, this may need to be adjusted depending on how tricky your lines are (more than one per line, other stuff on the line and so forth) but this "give me a list of all possible violations then remove those that aren't violations" is a tried and tested method.
For example (as Kent suggests), you may want to ensure that the HTML files are all directly in the app
directories instead of possibly app/something/xyzzy.html
. In that case, you could simply adjust your second filter to ensure this:
grep '\.html' | grep -v '/app/[^/]*\.html'
Using [^/]*
(any number of non-/
characters) instead of .*
(any number of characters, including /
) will leave in those that don't have the HTML file directly in the app
directory.
It might also be useful to know which files contained unwanted references to the old path.
I'd do something like this (disclaimer: not tested! But I copied some of it from paxdiablo, so that part's probably right.)
find /path/to/files_to_check -type f -name "*.html" -exec grep '\.html' {} \; /dev/null | grep -v '/app/.*\.html'
The find
command searches a directory hierarchy for regular files with names ending in .html
. Adjust as necessary for your situation.
For each of these files, grep
is run with two file arguments: {}
representing the target path, and /dev/null
to get grep
to prefix the matching line with the filename it occurred in. From that, we strip out anything matching '/app/.*\.html'
. What's left is a list of lines that need to be fixed, along with the filenames where they were found.
or
use this grep -P '(?<!/app)/[^/]*\.html
test:
kent$ echo ".../app/a/b/x.html
.../foo/myapp/y.html
.../foo/app/z.html"|grep -P '(?<!/app)/[^/]*\.html'
.../app/a/b/x.html
.../foo/myapp/y.html
note that this will ignore ..../app/*.html
, but .../myapp/*.html
or .../app/foo/x.html
will be matched.
精彩评论