The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Convert Variable file to fixed file. abhi9845 Shell Programming and Scripting 9 09-24-2008 09:07 AM
Convert Variable file to fixed file. abhi9845 UNIX for Dummies Questions & Answers 1 09-15-2008 08:07 AM
variable in dos batch file chssastry Windows & DOS: Issues & Discussions 6 08-29-2008 03:38 AM
variable into file pppswing Shell Programming and Scripting 2 07-02-2008 05:55 AM
how to attach a variable from the 2nd file to the 1st file lo tan Shell Programming and Scripting 5 06-03-2008 03:58 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 05-13-2009
Mr.Lauren Mr.Lauren is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
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 (permalink)  
Old 05-13-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,331
Use double quotes, single quotes prevent the shell to expand the variable:


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

  #3 (permalink)  
Old 05-13-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514

Code:
sed  "N c $variable" FILE

  #4 (permalink)  
Old 05-14-2009
Mr.Lauren Mr.Lauren is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
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 (permalink)  
Old 07-03-2009
jansonx jansonx is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 1
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0