In GNU Grep or another standard bash command, is it possible to get a resultset from regex? -
consider following:
var="text more text , yet more text" echo $var | egrep "yet more (text)"
it should possible result of regex string: text
however, don't see way in bash grep or siblings @ moment.
in perl, php or similar regex engines:
$output = preg_match('/yet more (text)/', 'text more text yet more text'); $output[1] == "text";
edit: elaborate why can't multiple-regex, in end have regex multiple of these (pictured below) need able of them. eliminates option of using lookahead/lookbehind (as variable length)
egrep -i "([0-9]+) +$user +([0-9]+).+?(/tmp/flash[0-9a-z]+) "
example input requested, straight lsof (replace $user "j" input data):
npviewer. 17875 j 11u reg 8,8 59737848 524264 /tmp/flashxxu8pvmg (deleted) npviewer. 17875 j 17u reg 8,8 16037387 524273 /tmp/flashxxibh29f (deleted)
the end goal cp /proc/$var1/fd/$var2 ~/$var3
every line, ends "downloading" flash files (flash used store in /tmp drm'd up)
so far i've got:
#!/bin/bash regex="([0-9]+) +j +([0-9]+).+?/tmp/(flash[0-9a-za-z]+)" echo "npviewer. 17875 j 11u reg 8,8 59737848 524264 /tmp/flashxxyovs8s (deleted)" | sed -r -n -e " s%^.*?$regex.*?\$%\1 \2 \3%p " | while read -a array echo /proc/${array[0]}/fd/${array[1]} ~/${array[2]} done
it cuts off first digits of first value return, , i'm not familiar enough sed see what's wrong.
end result downloading flash 10.2+ videos (including, perhaps, encrypted ones):
#!/bin/bash lsof | grep "/tmp/flash" | sed -r -n -e " s%^.+? ([0-9]+) +$user +([0-9]+).+?/tmp/(flash[0-9a-za-z]+).*?\$%\1 \2 \3%p " | while read -a array cp /proc/${array[0]}/fd/${array[1]} ~/${array[2]} done
edit: @ other answer simpler bash-only solution.
so, here solution using sed
fetch right groups , split them up. later still have use bash read them. (and in way works if groups not contain spaces - otherwise had use divider character , patch read
setting $ifs
value.)
#!/bin/bash user=j regex=" ([0-9]+) +$user +([0-9]+).+(/tmp/flash[0-9a-za-z]+) " sed -r -n -e " s%^.*$regex.*\$%\1 \2 \3%p " | while read -a array cp /proc/${array[0]}/fd/${array[1]} ~/${array[2]} done
note had adapt last regex group allow uppercase letters, , added space @ beginning sure capture whole block of numbers. alternatively here \b
(word limit) have worked, too.
ah, forget mentioning should pipe text script, this:
./grep-result.sh < grep-result-test.txt
(provided files named this). instead can add < grep-result-test
after sed
call (before |
), or prepend line cat grep-result-test.txt |
.
how work?
sed -r -n
calls sed in extended-regexp-mode, , without printing automatically.-e " s%^.*$regex.*\$%\1 \2 \3%p "
gives sed program, consists of singles
command.- i'm using
%
instead of normal/
parameter separator, since/
appears inside regex , don't want escape it. the regex search prefixed
^.*
, suffixed.*$
grab whole line (and avoid printing parts of rest of line).note
.*
grabs greedy, have insert space our regexp avoid grabbing start of first digit group too.- the replacement text contains of 3 parenthesed groups, separated spaces.
- the
p
flag @ end of command says print out pattern space after replacement. since grabbed whole line, pattern space consists of replacement text.
- i'm using
so, output of sed example input this:
5 11 /tmp/flashxxu8pvmg 5 17 /tmp/flashxxibh29f
this more friendly reuse, obviously.
now pipe output input while loop.
read -a array
reads line standard input (which output sed, due our pipe), splits words (at spaces, tabs , newlines), , puts words array variable.we have written
read var1 var2 var3
instead (preferably using better variable names), first 2 words put$var1
,$var2
,$var3
getting rest.- if
read
succeeded reading line (i.e. not end-of-file), body of loop executed:${array[0]}
expanded first element of array , similarly.
- when input ends, loop ends, too.
Comments
Post a Comment