Skip to content

Instantly share code, notes, and snippets.

@takumikinjo
Created October 31, 2012 17:17
Show Gist options
  • Save takumikinjo/3988418 to your computer and use it in GitHub Desktop.
Save takumikinjo/3988418 to your computer and use it in GitHub Desktop.
My favorite shell functions.
# . <(curl -L http://goo.gl/pdDKD)
# e.g.: jked /etc/hosts:3:
function jked {
file=`echo "$@" | sed 's/[:]/ /g' | awk '{print $1}'`
n=`echo "$@" | sed 's/[:]/ /g' | awk '{print $2}'`
if echo "$n" | grep '^[0-9]*$'; then
vi "$file" +"$n"
else
vi "$file"
fi
}
# e.g.: find | xargs grep -n \\\.func\( | jkspre class def
function jkspre {
while read i; do
if echo "$i" | egrep -q '^[^ ].*:[0-9]+:'; then
filepath=$(echo $i | cut -d\: -f1)
lineno=$(echo $i | cut -d\: -f2)
elif echo "$i" | egrep -q '^[^ ].*\-[0-9]+\-'; then
filepath=$(echo $i | cut -d- -f1)
lineno=$(echo $i | cut -d- -f2)
fi
bylineno=$(
cat <<EOF | ed $filepath 2>&1 | tail -n 1 | perl -pe 's/^(\d+).*/$1/'
$lineno
?${1}
n
EOF
)
if [ $bylineno -lt $lineno ]; then
cat <<EOF | ed -s $filepath | perl -pe 's/^(\d+)(.*)/'$(echo $filepath | perl -pe 's/\//\\\//g')':$1:$2/' | perl -pe 's/\t//'
${bylineno}n
EOF
fi
done
}
# e.g.: find | xargs grep -n FIXME | jksnext def
function jksnext {
while read i; do
if echo "$i" | grep -q -E ':[0-9]+:'; then
filepath=$(echo $i | cut -d\: -f1)
lineno=$(echo $i | cut -d\: -f2)
elif echo "$i" | grep -q -E '\-[0-9]+\-'; then
filepath=$(echo $i | cut -d- -f1)
lineno=$(echo $i | cut -d- -f2)
fi
bylineno=$(
cat <<EOF | ed $filepath 2>&1 | tail -n 1 | perl -pe 's/^(\d+).*/$1/'
$lineno
/${1}
n
EOF
)
if [ $bylineno -gt $lineno ]; then
cat <<EOF | ed -s $filepath | perl -pe 's/^(\d+)(.*)/'$(echo $filepath | perl -pe 's/\//\\\//g')':$1:$2/' | perl -pe 's/\t//'
${bylineno}n
EOF
fi
done
}
# e.g.: find . -type f | xargs grep -Hn 'def pattern_*' | jksn '^end' -1
function jksn {
while read i; do
if echo "$i" | grep -q -E ':[0-9]+:'; then
filepath=$(echo $i | cut -d\: -f1)
lineno=$(echo $i | cut -d\: -f2)
elif echo "$i" | grep -q -E '\-[0-9]+\-'; then
filepath=$(echo $i | cut -d- -f1)
lineno=$(echo $i | cut -d- -f2)
fi
bylineno=$(
cat <<EOF | ed $filepath 2>&1 | tail -n 1 | perl -pe 's/^(\d+).*/$1/'
$lineno
/${1}
n
EOF
)
if [ "$2" = "-1" ]; then
bylineno=$(expr $bylineno - 1)
fi
if [ $bylineno -ge $lineno ]; then
cat <<EOF | ed -s $filepath | perl -pe 's/^(\d+)(.*)/'$(echo $filepath | perl -pe 's/\//\\\//g')':$1:$2/' | perl -pe 's/\t//'
${lineno},${bylineno}n
EOF
else
cat <<EOF | ed -s $filepath | perl -pe 's/^(\d+)(.*)/'$(echo $filepath | perl -pe 's/\//\\\//g')':$1:$2/' | perl -pe 's/\t//'
${lineno},\$n
EOF
fi
done
}
# e.g.: cat foo | jkindent 2
function jkindent {
n=${1:-2}
m=$(echo $n | sed 's/-//g')
s=$(for i in `seq 1 $m`; do echo -n ' '; done)
if [ $n -lt 0 ]; then
cat | expand | sed "s/^$s//"
elif [ $n -gt 0 ]; then
cat | expand | sed "s/^/$s/"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment