From ce0558a53ff7053df7e6e78c6356b7059392f1b0 Mon Sep 17 00:00:00 2001 From: hophacker Date: Tue, 6 Apr 2021 07:01:42 +0800 Subject: [PATCH] enable Replace to delete all lines of a pattern: Replace -s xxx -r --- zsh/functions.zsh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/zsh/functions.zsh b/zsh/functions.zsh index 017e892..74f6d90 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -106,6 +106,7 @@ function Replace () { -f File regex pattern -s Source pattern -d Destination pattern + -r Remove line --seperator= Seperator, # by default -h Display this message" } @@ -114,7 +115,8 @@ function Replace () { DST="" SEP=";" DEBUG=false - while getopts ":hf:s:d:-:" opt + REMOVE=false + while getopts ":rhf:s:d:-:" opt do case "${opt}" in -) @@ -132,6 +134,7 @@ function Replace () { ;; esac;; f) FILE_REGEX=$OPTARG ;; + r) REMOVE=true ;; s) SRC=$OPTARG ;; d) DST=$OPTARG ;; h) usage; return 0 ;; @@ -141,10 +144,15 @@ function Replace () { done shift $(($OPTIND-1)) 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 - 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 - echo $MATCHED_FILES | xargs sed -i s${SEP}$SRC${SEP}$DST${SEP}g + echo $MATCHED_FILES | xargs sed -i ${SED_CMD} fi } function git-change-module-remote() {