#!/bin/bash
read -p 'Username: ' uservar
match='<color="red" />'
text='this is only a test
so please be patient
<color="red" />'
echo "$text" | sed "s/$match/&$uservar\g"
so desireble output what i want is if user type MARIA
this is only a test
so please be patient
<color="red" />
MARIA
when try to execute it returns me
sed: -e expression #1, char 32: unknown option to `s'
Where is a mistake, i couldnt find it.
It is working with "simple" words like PATIENT or some else, only dont work with this tags.
match='<color="red" />' contains a forward slash (/) which when expanded for the search it messes up with the parsing in sed.
Possible solutions:
Escape the offending character in the var
match='<color="red" \/>'
Or change the search/replace delimiter characters to something else
sed "s#$match#&$uservar#g"
By the way, the backslash before pattern flag g is not correct, it should be a forward slash /g and I do not see any reason for it to exist, in this case.
Thanks.
Idea behind all this is to read user input in numbers from 4-8, and each number present a different html tag and with more options.
So if user input is 6, it will added 4,5 and 6 after $match string.
------ Post updated at 03:07 AM ------
But now for the test purposes i wanted to put maria if user input 1, but i cant, why?
case $uservar in
1) echo "$text" | sed "s/$match/&MARIA/" ;;
2) echo -e "\nYou are 2" ;;
*) echo dont know ;;
esac
#!/bin/bash
read -p 'Enter number [1-8]: ' uservar
match='<color="red" \/>'
text='this is only a test
so please be patient
<color="red" />'
echo "$text" | sed "s/$match/&\n$uservar/"
------ Post updated at 04:04 AM ------
I will deal with cases and echo (just for test, idea is to put into file) to see results
#case $uservar in
# 1) echo "$text" | sed "s/$match/&$uservar/" ;;
# 2) echo -e "\nYou are 2" ;;
# *) echo dont know ;;
#esac
I will try to combine with user input and variable match and text ehich i said earlier
------ Post updated at 08:58 AM ------
i did it like this
#!/bin/bash
read -p 'Enter number:' new
match='<color="red" \/>'
text='text='this is only a test
so please be patient
<color="red" />''
text0='<h1> <!-- COmment -->
this is a test
</h1>'
text1="MARIA"
text2="JACK"
text3="BRAD"
ARR1=( 0 $text0 $text1 $text2 $text3 )
uservar=$new
for (( i=1; i<=uservar; i++)); do echo "$text" | sed "s/$match/&\n ${ARR1}/" ;done