Skip to content

Commit

Permalink
refactor: Improve sed pattern matching in line_in_file function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Aug 20, 2024
1 parent 949a542 commit 7290729
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,11 @@ line_in_file() {
echo "Error: Replace action requires exactly two arguments (search and replace)" >&2
return 1
fi
if grep -qF -- "${args[0]}" "$file"; then
if grep -q -- "^${args[0]}" "$file"; then
# Escape forward slashes in the replacement string
local escaped_replace=$(echo "${args[1]}" | sed 's/\//\\\//g')
sed_inplace "s/^.*${args[0]}.*$/${escaped_replace}/" "$file"
# Match lines that start with the search term, followed by anything
sed_inplace "s/^${args[0]}.*$/${escaped_replace}/" "$file"
else
echo "${args[1]}" >> "$file"
fi
Expand Down

0 comments on commit 7290729

Please sign in to comment.