Problems with ampersand (&) in sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with ampersand (&) in sed command
# 1  
Old 09-05-2016
Code Problems with ampersand (&) in sed command

Hello everybody,

I have a Problem with sed command.
I want to replace a defined string with a string from a database field (dynamic).

e.g.
Code:
sed -i -e 's/%NAME%/'"$HNAME"'/g'

The Problem is that the $HNAME variable can contain Special characters like '&'
e.g.
Code:
HNAME="AH Kruger & Co. KG"

so I get the Output
Code:
"AH Kruger %NAME% Co. KG"

sed replace the ampersand with the searchstring.

Is it possible to tell sed not to modify the replacestring?

Thx for your help... Smilie

PS: Sorry for my bad english...

Last edited by rbatte1; 09-05-2016 at 01:31 PM..
# 2  
Old 09-05-2016
You'd need to escape the & character. In a recent shell this could be done like
Code:
sed "s/%NAME%/${HNAME//&/\\&}/g" file

.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Nohup with ampersand & process gets disconnected

For years I have spawned shell scripts with nohup and ampersand and they run whether or not I stay logged in. Recently a client told us that we had to set a keep alive timeout on all of our Redhat 7.6 Linux servers. Our sysadmin set the following parameters in the sshd_config file on all of our... (10 Replies)
Discussion started by: gandolf989
10 Replies

2. Shell Programming and Scripting

sed Ampersand

I want to add the character "<" to the end of each line of input using the & function in SED. Something like: sed 's/.*/&\</' It's important to use the &, not another method, because I want to know what I'm doing wrong. Thanks (5 Replies)
Discussion started by: rlopes
5 Replies

3. Shell Programming and Scripting

problems with unix & command

My objective is to check the value of a variable which I will set it in the command line after sleep time mentioned in the script.I have executed my script in the background then pressed Enter.I have set temporary variable like below export PAUSE="PAUSE".I am not getting the value of the variable... (3 Replies)
Discussion started by: liyakathali
3 Replies

4. Homework & Coursework Questions

sed & cut command issues

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: After using the egrep command to pull certain lines from the asg5f1 (creating the asg5f1c file), I am required... (1 Reply)
Discussion started by: robrom78
1 Replies

5. Shell Programming and Scripting

applescript & grep - sed command

I'm new using Unix commands in applescript. The following script you choose different folders with PDfs, get file count of PDfs on chosen folders, & write the results in text file. set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with... (0 Replies)
Discussion started by: nellbern
0 Replies

6. Shell Programming and Scripting

echo & sed problems

I need a script to to instert the following line into my postfix master.cf file: flags=Rq user=filter argv=/etc/postfix/disclaimer -f ${sender} -- ${recipient}if I use echo I get the line truncated: :~ echo flags=Rq user=filter argv=/etc/postfix/disclaimer -f ${sender} -- ${recipient}... (3 Replies)
Discussion started by: barrydocks
3 Replies

7. Shell Programming and Scripting

Perl & Sed command -- Out of Memory Error

Experts, We used to receive our source files with '~^' as row delimiter. This file contains 2500K records and two of the columns having value in HTML formats within the file. While running the below commands against the file, we are encountering out of memory, could you please help to... (3 Replies)
Discussion started by: srivijay81
3 Replies

8. Shell Programming and Scripting

Understanding ampersand (&) usage in the command

Please explain the usage of ampersand in the following command who & echo "Total number of users are `who|wc -l`" What I understand is that ampersand is used to run some process in the background. And, what I am expecting from this command is "Output of who should be displayed on the... (2 Replies)
Discussion started by: Shan_u2005
2 Replies

9. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

10. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies
Login or Register to Ask a Question