Skip to content

Instantly share code, notes, and snippets.

@nvarun
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvarun/9575155 to your computer and use it in GitHub Desktop.
Save nvarun/9575155 to your computer and use it in GitHub Desktop.
#!/bin/ksh
##DESC======================================================================
### Extracts block of code/section based on two arguments;
### 1 - Source Code File, or Git Diff Output
### 2 - Extract Pattern
##======================================================================
function fn__extrt_block {
test $# -ne 2 && exit 1
test -f $1 && fv_iputFile=$1 || exit 2
fv_srchPtrn="$2"
fv_funcWorkDir=$HOME/${0}
mkdir -p ${fv_funcWorkDir} && cd ${fv_funcWorkDir}
grep -n "${fv_srchPtrn}" ${fv_iputFile} | cut -d':' -f1 > outfile
while true
do
echo $(head -2 outfile) | read LB UB
test ! -z ${UB} && UB=`echo ${UB} - 1 | bc`
echo "${LB},${UB:-\$}p" > sed_scpt
sed -n -f sed_scpt ${fv_iputFile} > file__${LB}_${UB:-$}
sed '1d' outfile > outfile.n
mv outfile.n outfile
test ! -s outfile && break
done
rm -f outfile* sed_scpt
echo "INFO: Listing Generated Files"
ls -ltr file__*
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment