-
-
Save colrichie/6746236 to your computer and use it in GitHub Desktop.
FSED : flexible sed (looks like the fgrep)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
###################################################################### | |
# | |
# FSED : flexible sed (looks like the fgrep) | |
# | |
# Written by Rich Mikan (richmikan[at]richlab.org) at 2014/04/08 | |
# | |
# Usage : fsed <pattern_str> <substitute_str> <file> | |
# | |
# * fsed is more flexible substituter than the sed command. This | |
# ignores all of the functions which every meta-character of regular | |
# expression has, so that you can very easily substitute strings | |
# include various meta-characters, e.g. ^, $, \, /, &, and so on. | |
# | |
###################################################################### | |
# print the usage and exit | |
print_usage_and_exit () { | |
cat <<__USAGE 1>&2 | |
Usage : ${0##*/} <pattern_str> <substitute_str> | |
Version : Tue Apr 08 01:38:30 JST 2014 | |
__USAGE | |
exit 1 | |
} | |
# parse the arguments | |
if [ \( $# -le 1 \) -o \( $# -ge 4 \) ]; then | |
print_usage_and_exit | |
elif [ $# -eq 3 ]; then | |
if [ \( -f "$3" \) -o \( -c "$3" \) -o \( -p "$3" \) -o \( "_$3" = '_-' \) ]; then | |
file=$3 | |
else | |
echo '*** No such file found' 1>&2 | |
print_usage_and_exit | |
fi | |
else | |
file='-' | |
fi | |
pat=$(printf '%s' "$1" | | |
sed 's/\([].\*/[]\)/\\\1/g' | | |
sed 's/^\^/\\^/' | | |
sed 's/\$$/\\$/' ) | |
sub=$(printf '%s' "$2" | | |
sed 's/\([\&/]\)/\\\1/g' ) | |
# transformation | |
if [ "_$file" = '_-' ]; then | |
sed "s/$pat/$sub/g" | |
else | |
sed "s/$pat/$sub/g" "$file" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2013/10/15:
(1)made the code simpler at the sed command lines
(2)made the code acceptable to named pipe files
2013/10/16:
Fix a bug about the file existence check
2013/12/24:
Fix a bug on FreeBSD