X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=blobdiff_plain;f=lib.sh;h=474f638c42c8fc858418656ca59ad73e9adab8da;hp=72040244499bd585ebfbbd6a2edac57048331ddc;hb=0d6b1deba13390ed289d4c933e0a05da7d031e5e;hpb=a82683769e49aba668f48c8cbe6d8efd9d940b4b diff --git a/lib.sh b/lib.sh index 7204024..474f638 100644 --- a/lib.sh +++ b/lib.sh @@ -72,13 +72,49 @@ cmd_i() { sed_i() { cmd_i sed "$@" } +grep_i() { + cmd_i grep "$@" +} + +# Usage: perl_line_filter ... +# +# Run the perl command cmd on each line before printing it. +perl_line_filter() { + cmd="$1" + shift + + # Can't use -pe because it uses <> which treats the arguments as files. + perl -e "use strict; use warnings; while () { $cmd; print; }" "$@" +} + +# Usage: simple_cpp .. -- ... +# +# Replaces each FIRST (on word boundaries) with like a +# simple cpp replacement. +simple_cpp() { + cmd='my $i = 0;' + + for x; do + shift + + if test x"$x" = x--; then + break + fi + + cmd="$cmd s/\b$x\b/\$ARGV[\$i]/g; \$i++;" + done + + perl_line_filter "$cmd" -- "$@" +} # Print the current OS. The following OS are supported at the moment: +# # - Debian (debian) # - Gentoo (gentoo) # - Mac OS X (darwin) # - Solaris/OpenSolaris (sun) # - FreeBSD (freebsd) +# # If an unsupported OS is used an error is printed. os() { if test -f /etc/debian_version; then