sed -i '7 c\$variable' file ....(?!@#$%^&*!)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed -i '7 c\$variable' file ....(?!@#$%^&*!)
# 1  
Old 05-13-2009
sed -i '7 c\$variable' file ....(?!@#$%^&*!)

I have tried everything I can think of to get sed to change line N of a file to the contents of a variable. I have Googled the Internet, and I find lots of people telling how to use variables with the "Substitute" command, but no one telling how to use variables with the "Change" command.

I have considered the possibility of using the "Substitute" command with a wild card for existing text, but my search of the Internet tells me SED doesn't have wild cards in its repertoire, so it's difficult! There must be a better way.

As I noted in the subject, the basic form I used is (where N is a line number):
Code:
sed -i 'N c\$variable' FILE

Is this possible, or is there a different command that will do it? I am working in BASH scripts.

Thanks!
# 2  
Old 05-13-2009
Use double quotes, single quotes prevent the shell to expand the variable:

Code:
sed -i "N c $variable" FILE

# 3  
Old 05-13-2009
Code:
sed  "N c $variable" FILE

# 4  
Old 05-14-2009
Yes!! It works! Thank you Franklin (and devtakh)! What an relief--better than a Christmas present!

This is really a surprise to me because I assumed the hard quote configuration was "cast in concrete". I'll have to admit, though, that my conclusions came from reading various comments during my Internet search on "sed". The manual itself is pretty vague on this subject. Now, in retrospect, I see that at the end of the manual there is reference to the sed website where the problem of variables is discussed at length! If I'd seen that in the first place, I probably could have figured it out, and not wasted the space on the Forum.

But you didn't even use the "\"!!! That I wouldn't have concluded from the manual. I guess it proves how many ways there are of doing things. I'm going to go back to the sed website and do some more study. I am sure thankful for the function that "sed" provides.

By the way, was I ever also wide-eyed when I found out about Zenity, after seeing it referenced on the web, and then discovered it was already installed! Amazing!

Thanks again very much.
Lauren.
# 5  
Old 07-03-2009
Question It doesn't seem to work well under Solaris

First, -i option was not supported, so the above sed -i "N c $var " datafile is not portable.
Second, even if the -i option is not provided, using sed -i "N c[\] $var" datafile, still it cannot work.
The solution might be,
sed "N c\\
$var" datafile
which I tested under Solaris.

John, Hong

p.s.
sed "N s/.*/$var/" datafile also works out.

Last edited by jansonx; 07-03-2009 at 07:01 AM.. Reason: I got the answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

2. 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

3. Shell Programming and Scripting

[gnuwin32] sed & variable %TIME%

HI.. I made ".bat" in windows 2003 , with set TIEMPO1= %TIME% | sed -e "s/://g" -e "s/,//g" echo valor de tiempo1 = %TIEMPO1% when i execute this, the result is but if i open cmd, and execute en line command this.. G:\>echo %TIME% | sed -e "s/://g" -e "s/,//g" 12390841 anyone have... (5 Replies)
Discussion started by: upszot
5 Replies

4. UNIX for Dummies Questions & Answers

Remove part of file name with sed & mv

Ok, so I have bunch of files that are named "orange__file_name.asm" and I want to batch rename them to "file_name.asm" I know that using "ls | sed s/orange__//" will get rid of the part of the file name I do not want. But how do I combine that with the mv command to actually do it? Thanks JG (5 Replies)
Discussion started by: john galt
5 Replies

5. Shell Programming and Scripting

Parse text file in shell & store to variable

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is in constant. And there is only 1 BRXXX in a file at a given time. .txt file: BR276828... (1 Reply)
Discussion started by: script2010
1 Replies

6. Shell Programming and Scripting

SED/AWK file read & manipulation

I have large number of data files, close to 300 files, lets say all files are same kind and have extension .dat , each file have mulitple lines in it. There is a unique line in each file containing string 'SERVER'. Right after this line there is another line which contain a string 'DIGIT=0',... (4 Replies)
Discussion started by: sal_tx
4 Replies

7. UNIX for Dummies Questions & Answers

using sed to write horizontally & sequentially in a file

Hey there I have two commands to get exactly the data i want but.. i want to write them into a file side by side and in the same order so that they always match. So what i'm hoping to learn from this thread is some of the different ways one could take the output of grepped data and write them in... (7 Replies)
Discussion started by: phpfreak
7 Replies

8. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

9. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

10. 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
Login or Register to Ask a Question