sed command not accepting variable in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command not accepting variable in shell script
# 1  
Old 07-26-2014
Linux sed command not accepting variable in shell script

I am using a shell script in fedora linux. While calling to the shell I am also passing an argument (var1=0.77) like shown below

Code:
sh gossip.sh var1=0.77

in the shell following command is written (which doesn't work)

Code:
sed - i -e 's@prob=@prob="$var1";//@g' file.txt

Actually i want the above command to be interpretted as

Code:
sed - i -e 's@prob=@prob=0.77;//@g' file.txt

However the above command is working fine and doing following

file.txt(INPUT)
==========

prob=0.2;

file.txt(OUTPUT)
==========

prob=0.77;//0.2;


That is the output i want using variable (i.e. $var1) passed as an argument through shell invoking command (i.e. sh gossip.sh var1=0.77). Please help.
# 2  
Old 07-27-2014
Is this a homework assignment?

What shell are you using?

The sed command that you have shown us would give you a syntax error, not behave as you have suggested?

What is in gossip.sh besides the sed command you've shown us?
# 3  
Old 07-27-2014
I have attached the sample shell and sample text file. These are doing the same as i mentioned in my post without any error.
# 4  
Old 07-27-2014
You didn't answer the question: Is this a homework assignment? If this is a homework assignment, you need to refile this problem in the Homework and Coursework Questions forum and fill out the homework template completely from the Rules for Homework and Coursework Questions Forum.

If this isn't a homework assignment, please explain how this code will be used.

There is a HUGE difference between the sed command you posted before:
Code:
sed - i -e 's@prob=@prob=0.77;//@g' file.txt

and the code you attached to your last message:
Code:
sed -i -e  's@prob=@prob=0.77;//@g' test3.txt

(and I'm not referring to the fact that you're editing a different file).
# 5  
Old 07-27-2014
This is not an homework assignment. I am implementing a routing protocol for which i need to run the simulation many times with changed values (Changing the value of variable $var1).

So, would you mind to answer/help please?
# 6  
Old 07-27-2014
You cannot pass variables like this to shell (but to awk - are you mixing it up?).
Instead you pass the values (command arguments).
In the script they are available as positional parameters $1 $2 ...
# 7  
Old 07-27-2014
In addition to what MadeInGermany said, shell variables are not expanded inside single quotes (even if you have double quotes inside the single quotes). You could change gossip.sh to:
Code:
sed -i -e "s@prob=@prob=$var1;//@g" file.txt

(note the changes of single quotes to double quotes and the removal of the previous double quotes) and invoke it as:
Code:
sh var1=0.77 gossip.sh

(putting var1 into the environment in which gossip.shruns) or, more conventionally, change gossip.shto:
Code:
var1="$1"
sed -i -e "s@prob=@prob=$var1;//@g" file.txt

or, more simply:
Code:
sed -i -e "s@prob=@prob=$1;//@g" file.txt

and invoke it as:
Code:
sh gossip.sh 0.77

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

2. Shell Programming and Scripting

How to use a shell variable on sed command?

Hie everyone i have a complex sed command line which work fine for a static value given example sed -n '/SQL ID\:\ 1111111111111/!b;:a;/\*\*\*\*\*\*\*\*\*\*\*\*/!{$!{N;ba}};{/*/p}' ./MyInputFile.txt what i want is something like that sed -n '/SQL ID\:\... (2 Replies)
Discussion started by: ade05fr@yahoo.f
2 Replies

3. Shell Programming and Scripting

Shell script variable $1 used with put command

I have the following script used, i am new to shell scripting. tryign to understand. in the put $BASE_FOLDER/$base_name holds which path. What does it mean by $1 second path in put command is it constructing this path: /user/hive/warehouse/stage.db/$1 what is $1 holding in above path. ... (2 Replies)
Discussion started by: cplusplus1
2 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

shell script/sed command help

First off I have read the man pages for sed and am still having trouble working on a script to remove portions of a log: My goal is to take a log file to be emailed, read the file and strip the portions away AFTER the line MIME-Version:1.0 and strip away until it to the line starting with... (4 Replies)
Discussion started by: murphybr
4 Replies

6. Shell Programming and Scripting

Need help using sed command in shell script?

Hello, i want to take the input from user and according to that variable's value search in file emp.lst. Here is what i came up with echo -e "Enter string to be searched :\c" read str sed -n '/\$str/p' emp.lst this is not working! any idea why?Thanks in advance! :) (4 Replies)
Discussion started by: salman4u
4 Replies

7. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

8. Shell Programming and Scripting

Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command: subst1 old-pattern new-pattern filename Here is my shell script: #!/bin/bash # subst1 ARGS=3 E_BADARGS=65 if then echo "Usage: `basename $0`... (1 Reply)
Discussion started by: Todd88
1 Replies

9. Shell Programming and Scripting

Accepting user input in Bourne shell and using sed

He guys. Basically I want to make a script that can add, delete and view stuff in a external file called config.txt. I can open it up in Joe but im not sure how to read in the user input or using commands automatically in joe to edit, save then quit. Problem area below: 1) echo "Add... (1 Reply)
Discussion started by: Pits
1 Replies

10. Shell Programming and Scripting

how to give a variable to a command in shell script...

hi all... I am executing a comman in a shell script but the command needs a user input of character 'y' as input so it stops in between...may i know is there is any way of giving that character as input in the shell script itself???...thanks in advance.... (6 Replies)
Discussion started by: santy
6 Replies
Login or Register to Ask a Question