Examples
1 min

Use unix grep to conduct a web search on a static site

the web server code would have to execute the command-line program

grep -R --exclude-dir=versions --include='*.txt' "Test Post 8Apr2016 1130" .

the search would start in document root.

check string to permit only alphanumeric, underscore, and hyphen. maybe the period.

http://www.perlmonks.org/bare/?node_id=114642

Examples

@ grep -i -R --exclude-dir=versions --include='*.txt' "4APR2016" .@

./note-4apr2016-2102-edt.txt:# Note: 4Apr2016 2102 EDT
./2016/04/04/test-post-4apr2016-1312.txt:# test post 4apr2016 1312
./2016/04/04/test-1313.txt:# test post 4apr2016 1313
./index.txt:* [Note: 4Apr2016 2102 EDT](/note-4apr2016-2102-edt.html)
./tag-note.txt:* [Note: 4Apr2016 2102 EDT](/note-4apr2016-2102-edt.html)


grep -i -R --exclude-dir=versions --include=*.txt "comet" .


./2016/04/17/test-post-17apr2016-2103.txt:this is a sentence that mentions my old cat Comet.
./2016/04/17/test-post-17apr2016-2103.txt:asf jlajsflja lfjal fjla jf  lja sf af but this one does mention COMET again, along with some gibberish text a ljaslfj al ksjflkaj fja lja fjalf jal fjalj flaj fl jalf jlaj fll ajsf jl fjljf 
./2016/04/17/test-post-17apr2016-2103.txt:finally, i miss comet who passed away in May 2010 at the age of 18-years-old. almost six years ago already. 

if want to stop the search on each file after only one match, then add the -m 1

grep -i -R -m 1 --exclude-dir=versions --include=*.txt "comet" .

Only the first hit will get displayed.

perl script example not executed via the web server.


#!/usr/bin/perl -wT

use strict;

my $string="comet"; # actually comes from CGI

$string=~s/[^\w]//; # remove non word chars

# clean up environment
 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 $ENV{'PATH'} = '/bin:/usr/bin';


if (open (GREP, "-|")) {
    print <GREP>;
} else {
    exec ("grep", "-i", "-R", "--exclude-dir=versions", "--include=*.txt", $string, ".") || die "Error exec'ing command",
"\n";
}
close (GREP);

http://www.gnu.org/software/grep/manual/grep.html

From JR's : articles
248 words - 1986 chars - 1 min read
created on
updated on - #
source - versions



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Nov 18, 2024 - 11:15 p.m. EST