Issue
The following bash
is used to replace the "$source_product
" with "$source_product
" + "linefeed
" + "$target_product
".
Here is the bash script:
source_product="\$(LOCAL_DIR)/source \\"
target_product="\$(LOCAL_DIR)/target \\"
echo "source_product: $source_product"
echo "target_product: $target_product"
echo ""
echo "--- replace source with source+target ---"
echo ""
sed "s|${source_product}|${source_product}\n${target_product}|g" <<< "${source_product}"
Here is the output:
source_product: $(LOCAL_DIR)/source \
target_product: $(LOCAL_DIR)/target \
--- replace source with source+target ---
sed: -e expression #1, char 70: unterminated `s' command
Here is the expected output:
source_product: $(LOCAL_DIR)/source \
target_product: $(LOCAL_DIR)/target \
--- replace source with source+target ---
$(LOCAL_DIR)/source \
$(LOCAL_DIR)/target \
What's wrong in the above "sed
"? And how to fix?
Update:
Here is the file contains the "$(LOCAL_DIR)/source \
", I want to add the "$(LOCAL_DIR)/target \
" just below the "$(LOCAL_DIR)/source \
":
...
other format a
...
$(LOCAL_DIR)/product_foo \
$(LOCAL_DIR)/bar \
$(LOCAL_DIR)/hello \
$(LOCAL_DIR)/world \
$(LOCAL_DIR)/source \
$(LOCAL_DIR)/others_1 \
$(LOCAL_DIR)/others_2 \
$(LOCAL_DIR)/many_other_products \
...
other format b
...
I think sed
is the best solution.
Solution
You need to escape the backslashes (with more backslashes). You can do this using Bash substitution directly in your sed
command:
sed "s|${source_product//\\/\\\\}|${source_product//\\/\\\\}\n${target_product//\\/\\\\}|g" <<< "${source_product}"
Answered By - jmd_dk Answer Checked By - Dawn Plyler (WPSolving Volunteer)