"sed" command is not working in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "sed" command is not working in shell script
# 1  
Old 05-12-2009
"sed" command is not working in shell script

Hi All,

I am not much strong in shell scripting...
I am using sed command in my script to find and replace a string.......

This is how script looks :
#############
#!/usr/bin/ksh

CONFIG_FILE=iom_test.txt
FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"`
REPLACE=`echo "##NIS_FTP_SERVER1=123.456.iom.com"`
export CONFIG_FILE FIND REPLACE;

## Replace all Special characters[#, _, ., and =] and store it back to FIND
NEW_FIND=`echo "$FIND" | sed 's/\#/\\\#/g' | sed 's/\_/\\\_/g' | sed 's/\=/\\\=/g' | sed 's/\./\\\./g'`
FIND=$NEW_FIND

NEW_REPLACE=`echo "$REPLACE" | sed 's/\#/\\\#/g' | sed 's/\_/\\\_/g' | sed 's/\=/\\\=/g' | sed 's/\./\\\./g'`
FIND=$NEW_REPLACE

echo "sed -e 's/^$FIND/$REPLACE/g' $CONFIG_FILE"
mv $CONFIG_FILE $CONFIG_FILE.old
sed -e 's/^$FIND/$REPLACE/g' $CONFIG_FILE.old > $CONFIG_FILE
echo "Changes are done"

###########

Here my pb is, I am not getting any error but it is not doing any changes with sed command....
But if I give the sed command directly in the shell that works perfectly
like :
sed -e 's/^\#\#NIS\_FTP\_SERVER1\=123\.456\.iom\.com/##NIS_FTP_SERVER1=123.456.iom.com/g' iom_test.txt

Can anyone pls help me out??
Thanks in advance
# 2  
Old 05-12-2009
what is the contents of file iom_test.txt
# 3  
Old 05-12-2009
Couple things:
Why are you echoing and putting the entire sed command between quotes?
If you want sed to read the variables, the command should look like:
Code:
 sed "s/^$FIND/$REPLACE/g" ...

Also, ^$ means something to sed, blank line. Use ${VARIABLE} instead of $VARIABLE
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Query on using command "sed" for replacing text in bash shell

Re: Query on using command "SED" for replacing text in bash shell While using the command "sed" for find and replace, I wanted to know how one could find a constant and replace it with a variable inside the quotation syntax of sed? I wanted to replace constant 3 with variable name... (3 Replies)
Discussion started by: achandra81
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Using sed command to replace "|" with ^ for all *.dat files in a folder not working

I am trying to use the below sed command to replace all "|" to ^, in a folder had 50 dat files. when i tried with 1 file it worked but when i tried with wild card, is not working. sed -i 's/"|"/\^/g' *.dat Is this the proper way to use sed command thank you very much for help. (3 Replies)
Discussion started by: cplusplus1
3 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

8. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies
Login or Register to Ask a Question