Just for something different...
The following script does not use grep
the second time around. It relies only on the line numbers gathered by grep in the the first step, and uses sed
for the printout..
grep -HnZ
is used in the first step: H
for the filename, n
for the line number, and Z
for a non-text delimiter \x00
between the filename and linenumber.
I don't think that it will be much (if any) faster than running grep
over the files which were idendified in the first path, because each of the identified files need to be scanned in either case. Also it isn't accurate if anything relevant changes in the data set input in the first step. (This just caught my interest, so here it is..)
# create 2 test files.
printf '%s\n' {a..z} >junk1
printf '%s\n' {a..z} >junk2
# Make list of filenames and line numbers
# then convert the list into a shell script
# which uses 'sed' to list the lines
grep -HnZ "[gms]" junk1 junk2 |
# Make list of filenames and line numbers
awk -v"C=2" 'BEGIN{ FS="[\x00:]"
print "#!/bin/sh"
}
{ negC=$2-C; if (negC<1){negC=1}; posC=$2+C }
prev != $1 {
if( prev ) print prev_grp "\""
prev = $1
prev_grp = "<\"" $1 "\" sed -nr \"" \
negC"i -- ("negC","$2","posC") "$1"\n\t"negC","posC"{p;b};"
next
}
{ prev_grp = prev_grp" " \
negC"i -- ("negC","$2","posC") "$1"\n\t"negC","posC"{p;b};"
}
END{ if( prev ) print prev_grp "\"" }
'>junk.sh
chmod +x junk.sh
./junk.sh
This is the output of the initial grep
command, showing the null as \x00
junk1\x007:g
junk1\x0013:m
junk1\x0019:s
junk2\x007:g
junk2\x0013:m
junk2\x0019:s
Here is the generated script
#!/bin/sh
<"junk1" sed -nr "5i -- (5,7,9) junk1
5,9{p;b}; 11i -- (11,13,15) junk1
11,15{p;b}; 17i -- (17,19,21) junk1
17,21{p;b};"
<"junk2" sed -nr "5i -- (5,7,9) junk2
5,9{p;b}; 11i -- (11,13,15) junk2
11,15{p;b}; 17i -- (17,19,21) junk2
17,21{p;b};"
Here is the grep-like output (n,n,n) are linenumbers (from, matched, to)
-- (5,7,9) junk1
e
f
g
h
i
-- (11,13,15) junk1
k
l
m
n
o
-- (17,19,21) junk1
q
r
s
t
u
-- (5,7,9) junk2
e
f
g
h
i
-- (11,13,15) junk2
k
l
m
n
o
-- (17,19,21) junk2
q
r
s
t
u
It would be prettty simply to add colour, but it would be easier to use grep
, (unless this offers anything desirable).