Reputation: 133
I created a test file with the following:
<cert>
</cert>
I'm now trying to find this with grep and the following command, but it take forever to run. How can I search quickly for files that contain adjacent lines like these?
tr -d '\n' | grep '<cert></cert>' test.test
Upvotes: 0
Views: 96
Reputation: 50220
So, from the comments, you're trying to get the filenames that contain an empty <cert>..</cert>
element. You're using several tools wrong. As @iiSeymour pointed out, tr
only reads from standard input-- so if you want to use it to select from lots of filenames, you'll need to use a loop. grep
prints out matching lines, not filenames; though you could use grep -l
to see the filenames instead.
But you're only joining lines because grep
works one line at a time; so let's use a better tool. Here's how to search with awk:
awk '/<cert>/ { started=1; }
/<\/cert>/ { if (started) { print FILENAME; nextfile;} }
!/<cert>/ { started = 0; }' file1 file2 *.txt
It checks each line and keeps track of whether the previous line matched <cert>
. (!/pattern/
sets the flag back to zero on lines not matching /pattern/
.) Call it with all your files (or with a wildcard like *.txt
).
And a friendly suggestion: Next time, try each command separately (you've been stuck on this for hours and you still don't know what grep
does?). And have a quick look at the manual for the tools you want to use. Unix tools are usually too complex for simple trial and error.
Upvotes: 1