$Variable address in sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers $Variable address in sed
# 1  
Old 02-27-2006
$Variable address in sed

I'm attempting to append a line after a specific address with sed. If I type in the address eg.3 then it will append after the third line. I can't get it to work with a variable though. Does anyone know how to do it please?

sed '/3/a\
text' filename Smilie

sed '/\$address/a\
text' filename Smilie
# 2  
Old 02-27-2006
use " instead of '
# 3  
Old 02-27-2006
Afraid not; it gives me this error message if I replace ' with "

sed: -e expression #1, char 10: Extra characters after command
# 4  
Old 02-27-2006
You need a closing / before the closing double quote

sed -e "s/$address/text/" filename

Last edited by sssow; 02-27-2006 at 05:56 PM..
# 5  
Old 02-27-2006
Nope, still no result.
# 6  
Old 02-27-2006
Replacing the single quotes will work given the requirement is as vgersh and sssow are understanding it. Try posting some sample data.
# 7  
Old 02-27-2006
file1 is four lines "line 1, line 2, line 4, line 5"

I want to introduce a new line.

For example, this works:

sed '/2/a\
line 3' file1

This doesn't

variable=2
sed "/\$variable/a\
line 3/" file1

Nor this:
sed '/\$variable/a\
line 3/'file1

Nor this:
sed "/$variable/a\
line 3" file1

I've tried various combinations of " ' \ / but can't get it to accept the variable.

This is the error it usually come up with:
sed: -e expression #1, char 10: Extra characters after command
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

2. Shell Programming and Scripting

HELP with awk or sed. Need to replace all IP address by 2

Hi, In a file, I have several time <IP>232.0.1.164</IP> ... <IP>232.0.1.135</IP> I need to replace all the random IP addresses , by 239.0.0.1 and 239.0.0.2 , alternatively. I try this grep "<IP>" tsp.xml | awk '{if(NR % 2)print $0}' | cut -d"<" -f2 | cut -d">" -f2 ... (3 Replies)
Discussion started by: FredMo
3 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Get Local IP address using Sed, Awk

Hi All, how to get solaris box local ip addresss in variable, using sed or awk utlities. Thanks, Mani Muthu (7 Replies)
Discussion started by: k_manimuthu
7 Replies

5. UNIX for Dummies Questions & Answers

grep or sed help for a word and an IP address

I would like to grab only designates and the IP address next to it. I can also live with client-ip=xx.xx.xx.xx Any help is much appreciated. line to grep or sed: Received-SPF: pass (google.com: domain of support@uhb-hosting.de designates 80.67.28.12 as permitted sender)... (7 Replies)
Discussion started by: tigta09
7 Replies

6. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. UNIX for Dummies Questions & Answers

address variable content with csh

By using a csh, I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 ... (1 Reply)
Discussion started by: sobolev
1 Replies

8. Shell Programming and Scripting

using sed to get IP-address

Hi folks! I need to get an ip address of a string that has the same apperance in every case. The string looks like this: $node="node_ip:109.50.89.211; node:j20" What I want to do is to extract the IP-address from this string using regular expression. Since I havn't worked much with... (10 Replies)
Discussion started by: eeriale
10 Replies

9. Programming

Address of PATH variable

I want to find the address of the PATH environment variable. What is the best way to go about finding that address? Does the address of the PATH variable ever change, or is it constant? (11 Replies)
Discussion started by: Legend986
11 Replies

10. UNIX for Dummies Questions & Answers

passing ip address as a variable to script for ftp

Still a Beginner here .. Does anyone no how to get the IP address of the machine thats logged in (bearing in mind there will be others logged in) while they are logged in to the Unix server and pass this as a variable to a shell script so as I can FTP files to that machine via a shell script, at... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question