Using sed inside shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed inside shellscript
# 1  
Old 06-01-2016
Using sed inside shellscript

Hi All,

I am trying to use sed command inside a shell script.
The same sed command is working on command line, however its not working while using inside a shell script.
From various sources i found , it could be done by using -i option, but its not working as well.

Code:
sed 's/something/otherone/g' inputfile

its working perfectly filne while using on command line.

Code:
#! /bin/ksh
sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' full/path/inputfile

Tried using absolute path of sed, and inputfile but still its not working.
ANy pointer on this please.

NOTE: I am using SunOS

Thanks
Goutam
# 2  
Old 06-01-2016
Quote:
Originally Posted by gotamp
Hi All,
I am trying to use sed command inside a shell script.
The same sed command is working on command line, however its not working while using inside a shell script.
From various sources i found , it could be done by using -i option, but its not working as well.
Code:
sed 's/something/otherone/g' inputfile

its working perfectly filne while using on command line.
Code:
#! /bin/ksh
sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' inputfile
#/usr/bin/sed 's/something/otherone/g' full/path/inputfile

Tried using absolute path of sed, and inputfile but still its not working.
ANy pointer on this please.
NOTE: I am using SunOS
Thanks
Goutam
Hello gotamp,

If I am seeing it correctly you have space in between #! and /bin/ksh in your shebang, try without the space as #!/bin/ksh and let me know how it goes then.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-01-2016
Thanks R.Singh for correctly pointing it out.
I have done the changes accordingly, but still it does not work.
Not sure, if sed could be used this way inside a shell script.

THanks
Goutam
# 4  
Old 06-01-2016
Any error messages? Run with -x option set and post the result...
# 5  
Old 06-01-2016
Hi RudiC

Please find my shell script that uses sed
Code:
#!/bin/ksh
a=$1
b=$2
echo "a is $a , b is $b"
/usr/bin/sed 's/$a/$b/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

and this is the data contained in the file filetosedreplace
Code:
D1: cat /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

and now, this is the output when i run the script in debug mode
Code:
D1: sh -x sedinshell.ksh one ONE
a=one
b=ONE
+ echo a is one , b is ONE
a is one , b is ONE
+ /usr/bin/sed s/$a/$b/g /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

additional info
Code:
D1: uname -a
SunOS simsun2 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise

# 6  
Old 06-01-2016
Quote:
Originally Posted by gotamp
Hi RudiC

Please find my shell script that uses sed
Code:
#!/bin/ksh
a=$1
b=$2
echo "a is $a , b is $b"
/usr/bin/sed 's/$a/$b/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

and this is the data contained in the file filetosedreplace
Code:
D1: cat /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

and now, this is the output when i run the script in debug mode
Code:
D1: sh -x sedinshell.ksh one ONE
a=one
b=ONE
+ echo a is one , b is ONE
a is one , b is ONE
+ /usr/bin/sed s/$a/$b/g /user/iomaint/tempo/gtm/sed/filetosedreplace
the one should be replaced with ONE

additional info
Code:
D1: uname -a
SunOS simsun2 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise

Hello gotamp,

You are passing variables to the script, so could you please try following(haven't tested though).
Code:
/usr/bin/sed 's/'${a}'/'${b}'/g' /user/iomaint/tempo/gtm/sed/filetosedreplace
OR
/usr/bin/sed 's/"{$a}"/"{$b}"/g' /user/iomaint/tempo/gtm/sed/filetosedreplace

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 06-01-2016
Your samples in post#5 show a TOTALLY different picture that you have in post#1!

Within single quotes, the shell does NOT expand variables, so sed 's/$a/$b/g' tries to replace the string constant "$a" (which, of course, is not found in your file) with the string constant "$b" - as you can see in your xtrace output as well. Try double quotes instead, or the proposal by RavinderSingh13.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use of sed inside perl

$string="test.binary.value"; $toggle="test.binary.value=true"; host=test.server.com my $returnCode = system ( "ssh $host 'cp -p /tmp/testfile /tmp/testfile.bkup; sed -i 's/\$string.*/\$toggle/g' /tmp/testfile'" ); cat testfile test.binary.value=false Scenario: I am trying to... (3 Replies)
Discussion started by: Tuxidow
3 Replies

2. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

3. UNIX for Dummies Questions & Answers

sed inside awk

What I want to do is delete everything upto the last underscore (_) in column 2. awk '{ $2=$(echo $2 | sed 's/.*_//'); print $0}' Sed works fine if I echo the string into it, this doesnt work inside awk. What am I doing wrong? Similarly, how do I store the substring starting with a... (4 Replies)
Discussion started by: senhia83
4 Replies

4. Shell Programming and Scripting

Variable inside sed

Problem: Need to read contents from a file and use that value inside sed as avariable. sample code below. THe below code replaces contents inside file123 for matched string with "$x" value only. but we want the value of x which is read from TextFile.txt to go in there. cat TextFile.txt|while... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

5. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

6. Shell Programming and Scripting

remove newline between two string with sed command in unix shellscript

I have a file (test.dat) which contains data like this 459|199811047|a |b |shan kar|ooty| 460|199811047|a |bv |gur u|cbe| but I need it like: 459|199811047|a |b |shankar|ooty| 460|199811047|a |b |guru|cbe| While reading the data from this file, I don't want to remove newline from the end of... (4 Replies)
Discussion started by: jcrshankar
4 Replies

7. Shell Programming and Scripting

Need help using sed inside the loop.

Hi, i have written a script. it collects data based on the sql queries executed by it. i have multiple output files. after the output file is made i need to do some cosmetic changes in the files and then store them. i am unable to use sed conditions inside the loop. see below code for... (3 Replies)
Discussion started by: dazdseg
3 Replies

8. UNIX for Dummies Questions & Answers

SED inside while loop

Hi, im having problem creating a loop using my code: aside from the fact that the 1st variable (VAR) does not increment, it loops more than the expected output. for sample purposes, test csv contains 3 lines. #get number of lines in the file lines=$( wc -l < test.csv ) ... (5 Replies)
Discussion started by: paoie
5 Replies

9. Shell Programming and Scripting

sed Problem in Shellscript

Hi, i have a problem with sed i want to substitude a / to ",""," in the following pattern: number/number so if u have a file which contains -> "Streetname 9/8/6" it shouldn't substitude anything, only if it is "Streetname 9/6" to "Streetname 9","","6". Maybe you can help me, i try so many... (5 Replies)
Discussion started by: urukai
5 Replies

10. Shell Programming and Scripting

Using variable inside 'sed'

Dear all, How can I amend the following command to use variable inside 'sed' ? str=`sed -e 's/orig_string/${var}/g' ${sourcefile}` Thanks, Rocky (3 Replies)
Discussion started by: Rock
3 Replies
Login or Register to Ask a Question