Removing the </p> tag from grep output
I have I bash script that will find phones numbers inside .htm or .html files in a directory (or recursivly down if I want it) to find phone numbers in the format (ddd)ddd-dddd or ddd-ddd-dddd (Where d represents a digit).
This is my code:
find ./ -maxdepth 1 -regex ".*\(html\|htm\)$" | xargs grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[-]\?[0-9]\{3\}-[0-9]\{4\}'
The output is:
./dash_only_phone.htm:800-555-1212</p>
./paren_phone.htm:(800)555-1212</p>
I was wondering how I wou开发者_C百科ld change the grep command to remove the html p tag printout at the end.
Thanks,
If your grep
supports Perl Compatible Regular Expressions, as do GNU and OS X grep
:
grep -Po '(\([0-9]{3}\)|[0-9]{3})-?[0-9]{3}-[0-9]{4}(?=</p>)'
Note the changes in escaping (which are similar to or the same as for grep -E
).
Why not just pass the output through a sed
filter to remove it, as in the following transcript:
pax$ echo './dash_only_phone.htm:800-555-1212</p>' | sed 's?</p>$??'
./dash_only_phone.htm:800-555-1212
This will get rid of any </p>
sequences that appear at the end of a line.
You can just add the -o
switch to get the IP
find ./ -maxdepth 1 -regex ".*\(html\|htm\)$" | xargs grep -o '\(([0-9]\{3\})\|[0-9]\{3\}\)[-]\?[0-9]\{3\}-[0-9]\{4\}'
精彩评论