sed not outputing variable into new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed not outputing variable into new file
# 1  
Old 04-16-2013
sed not outputing variable into new file

Hi all,

I have a script that creates folders depending on what variables i enter when calling the script. This all works as expected but what i want to do is add some additional data into another file so that other scripts are aware of this file.

I've found the following sed command which works to my needs. It enters the given details into a given line
Code:
sed "36a\mv -v $foo/"$SHOW"\ $bar/$SHOW/" test2.sh > test3.sh

the problem i'm having is that when the sed command has run, $SHOW displays still in the new file as $SHOW. Is there something i'm doing wrong as to why $SHOW wont "resolve" to the text i need to to be?

also is there an easier way to output the results of SED back into the original file? Instead of having to manipulate it in such a way that i end up with the original file name?
# 2  
Old 04-17-2013
Would you mind showing us a sample input and output? Thanks
# 3  
Old 04-17-2013
First part is unclear what you are trying to do.

Second part solution is "sed -i" if your version of sed supports it.
# 4  
Old 04-17-2013
This may be solved by other solution than sed.
Show your input and what you like to get out, and what algorithmic you use to get the output.
# 5  
Old 04-17-2013
Your command works perfectly once the variables are correctly set. This does not mean it can't be improved...
# 6  
Old 04-17-2013
So my initial script has a case command in which calls up functions according to which option i select. Within one of those functions i have the sed code which is to take the input of $SHOW and apply it into the file.

so say $SHOW = test foobar
then the sed command should should run like:
Code:
sed "36a\mv -v $foo/"test foobar"\ $bar/test foobar/" test2.sh > test3.sh

So in tesh3.sh i should see around line 36 the following:
Code:
mv -v $foo/"test foobar"\ $bar/test foobar/

but i always get $SHOW appear in the final file and it never takes the input from $SHOW and replaces it. $foo and $ bar are part of the test3.sh script so all they need to do is be entered into the test3.sh file.

Other parts of the function works when $SHOW is called its just sed that doesn't like the input.

Hope that helps.

Last edited by Franklin52; 04-17-2013 at 08:13 AM.. Reason: Please use code tags
# 7  
Old 04-17-2013
It would help to show a complete test script.

Also, use code tags. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. Shell Programming and Scripting

Using echo, grep and wc and outputing to text file

My current line command is as follows: echo -n "text: " ; grep "blah text" ../dir1/filename | wc -l The output to the screen is as needed, but how do I print to a text file? (9 Replies)
Discussion started by: ncwxpanther
9 Replies

3. Shell Programming and Scripting

sed and variable as target file

for file in `ls /tmp/*_sw_list`; do /usr/bin/sed -i '' '1,/^Software\ Update/d' $file done In my script, this doesn't work. I can copy-and-paste it, and it works. Enabling debugging shows it is resolving the file name correctly... it isn't an issue with special characters in the filename. ... (3 Replies)
Discussion started by: jnojr
3 Replies

4. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

5. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

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

7. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

8. Shell Programming and Scripting

Having sed to read lines of a file with the use of a variable..Possible?

This i will print the second line of a file sed -n '2p' test2 The use of a variable is impossible here. a=1 while ; do line=`sed -n '$a p' test2` # do some things here with the line variable a=`expr $a + 1` done But the uotput of sed command is 'p'..... What can i do to use a variable... (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: Mr.Lauren
4 Replies

10. Shell Programming and Scripting

outputing sed to a variable

Hello, My appologies for asking a very basic question but... In a shell, I enter: echo tit | sed -e s/tit/tat/g This returns: tat as expected. But when I enter: set test = `echo tit | sed -e s/tit/tat/g` echo $test This returns an empty line. Why is this so and how... (3 Replies)
Discussion started by: flewis
3 Replies
Login or Register to Ask a Question