Insert a user input string after matched string in file
i am having file like this
Code:
#!/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
Code:
this is only a test
so please be patient
<color="red" />
MARIA
when try to execute it returns me
Code:
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.
Last edited by RudiC; 09-01-2018 at 07:10 PM..
Reason: Changed QUOTE to CODE tags.
#!/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
Code:
this is only a test
so please be patient
<color="red" />
MARIA
when try to execute it returns me
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
Code:
match='<color="red" \/>'
Or change the search/replace delimiter characters to something else
Code:
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.
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
Code:
match='<color="red" \/>'
Or change the search/replace delimiter characters to something else
Code:
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?
Code:
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
Code:
#case $uservar in
# 1) echo "$text" | sed "s/$match/&$uservar/" ;;
# 2) echo -e "\nYou are 2" ;;
# *) echo dont know ;;
#esac
I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string.
Line repeating working every 3 lines using code:
sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt
I cannot seem to find... (1 Reply)
I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match.
I have tried this command but it does not work when I put the command in a loop in a bash script:
... (12 Replies)
Hi
I have one thing I need advice on, and I don't know where to start so I have no sample code.
I want the user to provide input like: 1-3,6,7,9-11
When the input is like this, I want a string to be generated including all the numbers. In the example above, the string would look like:
1... (13 Replies)
Hi,
i want to know how to compare string of file with input string
im trying following code:
file_no=`paste -s -d "||||\n" a.txt | cut -c 1`
#it will return collection number from file
echo "enter number"
read " curr_no"
if ; then
echo " current number already present"
fi
... (4 Replies)
i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2
thanks in advance! (4 Replies)
Hi ,
I have input file and i want to extract below strings
<msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets
<resCode>3000</resCode> => 3000 needs to be extracted
<resMessage>Request time
getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae )..
and then make a directory of every string from the text file (/path/meggae/) --->if it matched..
then print every line from the... (3 Replies)
Hi,
I have a shell script to build components of a product. The follow snippet will explain what I am doing.
# !/bin/sh
for choice in "$@" ;
do
case $choice in
"o") echo "Calling $choice" ; o ;;
"i") echo... (8 Replies)