Insert a user input string after matched string in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a user input string after matched string in file
# 1  
Old 09-01-2018
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.
# 2  
Old 09-01-2018
Quote:
Originally Posted by tomislav91
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


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.
# 3  
Old 09-02-2018
Quote:
Originally Posted by Aia
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

it returns me just my $text variable
# 4  
Old 09-02-2018
Again, may depend on the contents of $match. What's it?
# 5  
Old 09-02-2018
Code:
#!/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

# 6  
Old 09-02-2018
Where exactly is the problem?
Code:
this is only a test
so please be patient
<color="red" />MARIA

# 7  
Old 09-02-2018
because when user input 1 it prints me only

Quote:
this is only a test
so please be patient
<color="red" />
MARIA is not printed.
Code:
case $uservar in
  1) echo "$text" | sed "s/$match/&MARIA/" ;;
  2) echo -e "\nYou are 2" ;;
  *) echo dont know ;;
esac

Code:
read -p 'Enter number [1-8]: ' uservar

------ Post updated at 04:24 AM ------

Sorry my mistake, my match string is not match within text, its because i tested it 1000 times.
SORRY!

I am having one maybe stupid questions.
Does it possible to merge cases?

so for example, if user input is 1 it print
Code:
MARIA

, if input 2, it print
Code:
MARIA
HELEN

is user input is 3 it print
Code:
MARIA
HELEN
JOHN

So this data is more complicated and long and I dont wanna to give that in cases, so if user input is 2, i dont want to echo
Quote:
MARIA
HELEN
I want something case1\nHELEN
and so on..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

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)
Discussion started by: Skonectthedots
1 Replies

2. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

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)
Discussion started by: jojojmac5
12 Replies

3. UNIX for Dummies Questions & Answers

Commenting a line matched with a specific string in a file

Hi, I would like to comment a line that matched a string "sreenivas" in a file without opening it. Thanks in advance. Regards, Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies

4. Shell Programming and Scripting

String generation from user input

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)
Discussion started by: Tobbev
13 Replies

5. Shell Programming and Scripting

Matching string from input to string of file

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)
Discussion started by: a_smith
4 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

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)
Discussion started by: engr.jay
4 Replies

8. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

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)
Discussion started by: sushmab82
14 Replies

9. Shell Programming and Scripting

save every line in log file with matched string

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)
Discussion started by: meggae
3 Replies

10. Shell Programming and Scripting

String extraction from user input - sh

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)
Discussion started by: vino
8 Replies
Login or Register to Ask a Question