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
# 8  
Old 04-17-2013
This is basically a copy of the script
Code:
SHOW="$2"

create-new(){
		mkdir "$bar/$SHOW"
		sed "36a\mv -v $foo/"$SHOW"\ $bar/$SHOW/" test2.sh > test3.sh
}

case "$1" in
        create)
                create-new
                ;;
        remote)
                remove
                ;;
esac

# 9  
Old 04-17-2013
Thanks. What are the contents of test2.sh and what is the expected contents of test3.sh after sed runs?
# 10  
Old 04-18-2013
If what you are trying to do is to create a directory and create a script (test3.sh) that is a copy of another script (test2.sh) with an mv command added after line 36 then I think the following does what you want. You haven't said what shell you're trying to use for this script, so I'm assuming a standards conforming shell such as ksh or bash. (I used ksh while I was testing this script.) Assuming that is correct, there are a couple of problems:
  1. You can't have a minus sign in a function name. (I changed create-new to create_new to solve this problem.)
  2. It is hard to get the quotes correct using sed to transform test2.sh into test3.sh. (In this case I found it much easier to use a here-document with ed instead of trying to get the editing command arguments specified correctly with sed.)
  3. I added an additional case to provide a usage message (since I kept forgetting to add arguments while I was testing the script.
  4. I added matching ('s to the cases in your switch because I use showmatch while editing scripts and I find it easier to read when I do that.
Anyway, does this do what you're trying to do?
Code:
#!/bin/ksh
bar=to #Added because the script needs a value for bar
foo=from #Added because the script needs a value for foo
me=${0##*/} #Added for use in the usage message
SHOW="$2"

create_new(){
        mkdir "$bar/$SHOW"
        ed -s test2.sh <<-EOF
                36a
                mv -v "$foo/$SHOW" "$bar/$SHOW/"
                .
                w test3.sh
        EOF
}

case "$1" in
        (create)
                create_new ;;
        (remote)
                remove ;;
        (*)     printf "Usage:\t%s create file\n\t%s remove Who Knows What\n" \
                        "$me" "$me" ;;
esac

If not, you're going to have to actually show us exactly what you want to appear in test3.sh rather than showing us the commands that you're using to create a test3.sh that does not contain what you want.

I also note that your script has a case "remote" that calls the undefined function "remove". I assume one of these is misspelled, but since you haven't given us any indication of what should happen when $1 is not "create", I left it as is.
# 11  
Old 04-18-2013
You are telling us that (unquoted) $foo and $bar are being expanded, while (quoted) $SHOW is not.
Is the mkdir one line above working correctly, then?
Why don't you post an execution log with the -x option set?
# 12  
Old 04-19-2013
well i've managed to get it working now.

I took out the "" which was wrapped around $SHOW within sed

The only thing i have now is that when trying to run the test3.sh it fails due to the spaces as the file system uses "\" at the end of words with spaces.
# 13  
Old 04-19-2013
Probably best to start a new thread to solve this other interesting problem. Include the input, expected output, and what you have tried, using code tags.
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