I have some variables:
$begin=10
$end=20
how to pass them to sed
command.
sed -n '$begin,$endp' filename | grep word
sed -n '10,20p' filename | grep word
I have some variables:
$begin=10
$end=20
how to pass them to sed
command.
sed -n '$begin,$endp' filename | grep word
sed -n '10,20p' filename | grep word
The reason this doesn't work is that single quotes in shell code prevent variable expansion. The good way is to use awk:
awk -v begin="$begin" -v end="$end" 'NR == begin, NR == end' filename
It is possible with sed if you use double quotes (in which shell variables are expanded):
sed -n "$begin,$end p" filename
However, this is subject to code injection vulnerabilities because sed cannot distinguish between code and data this way (unlike the awk code above). If a user manages to set, say, end="20 e rm -Rf /;"
, unpleasant things can happen.