enable Replace to delete all lines of a pattern: Replace -s xxx -r

This commit is contained in:
hophacker 2021-04-06 07:01:42 +08:00
parent 751f5b8564
commit ce0558a53f

View File

@ -106,6 +106,7 @@ function Replace () {
-f File regex pattern -f File regex pattern
-s Source pattern -s Source pattern
-d Destination pattern -d Destination pattern
-r Remove line
--seperator= Seperator, # by default --seperator= Seperator, # by default
-h Display this message" -h Display this message"
} }
@ -114,7 +115,8 @@ function Replace () {
DST="" DST=""
SEP=";" SEP=";"
DEBUG=false DEBUG=false
while getopts ":hf:s:d:-:" opt REMOVE=false
while getopts ":rhf:s:d:-:" opt
do do
case "${opt}" in case "${opt}" in
-) -)
@ -132,6 +134,7 @@ function Replace () {
;; ;;
esac;; esac;;
f) FILE_REGEX=$OPTARG ;; f) FILE_REGEX=$OPTARG ;;
r) REMOVE=true ;;
s) SRC=$OPTARG ;; s) SRC=$OPTARG ;;
d) DST=$OPTARG ;; d) DST=$OPTARG ;;
h) usage; return 0 ;; h) usage; return 0 ;;
@ -141,10 +144,15 @@ function Replace () {
done done
shift $(($OPTIND-1)) shift $(($OPTIND-1))
MATCHED_FILES=`ag -Q $SRC -l -G $FILE_REGEX` MATCHED_FILES=`ag -Q $SRC -l -G $FILE_REGEX`
if $REMOVE; then
SED_CMD=\\${SEP}$SRC${SEP}d
else
SED_CMD=s${SEP}$SRC${SEP}$DST${SEP}g
fi
if [[ "$(uname)" == "Darwin" ]]; then if [[ "$(uname)" == "Darwin" ]]; then
echo $MATCHED_FILES | xargs sed -i '' "s$SEP$SRC$SEP$DST${SEP}g" echo $MATCHED_FILES | xargs sed -i '' "${SED_CMD}"
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
echo $MATCHED_FILES | xargs sed -i s${SEP}$SRC${SEP}$DST${SEP}g echo $MATCHED_FILES | xargs sed -i ${SED_CMD}
fi fi
} }
function git-change-module-remote() { function git-change-module-remote() {