-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfsed
executable file
·88 lines (76 loc) · 3.01 KB
/
fsed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
######################################################################
#
# FSED - Flexible sed (Looks Like the fgrep)
#
# Usage : fsed <pattern_str> <substitute_str> <file> [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.
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} <pattern_str> <substitute_str> <file> [file...]
Version : 2020-05-06 22:42:19 JST
(POSIX Bourne Shell/POSIX commands)
USAGE
exit 1
}
warning() {
${1+:} false && echo "${0##*/}: $1" 1>&2
}
# === Define misc variables ==========================================
LF=$(printf '\n_');LF=${LF%_} # <0x0a>
######################################################################
# Argument Parsing
######################################################################
# === Parse arguments (except files) =================================
case $# in [01]) print_usage_and_exit;; esac
case "$1" in *"$LF"*)
echo "${0##*/}: It is impossible to contain <0x0A> in <pattern_str>" 1>&2
exit 1
esac
pat=$(printf '%s\n' "$1" |
sed 's/\([].\*/[]\)/\\\1/g' |
sed '1s/^^/\\^/' |
sed '$s/$$/\\$/' )
sub=$(printf '%s\n' "${2}_" |
sed 's/\([\&/]\)/\\\1/g' |
sed 's/$/\\/' |
sed '$s/\\$//' )
sub=${sub%_}
shift 2
######################################################################
# Main Routine
######################################################################
(cat ${1+"$@"}; echo '') |
sed "s/$pat/$sub/g" |
awk 'BEGIN{ORS=""; OFS=""; #
getline line; #
print line; #
dlm=sprintf("\n"); #
while (getline line) {print dlm,line;} }' #