Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a string with sed 1

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
following script to perform string substition with 'sed' does not work, can anyone see what the problem is? thanks

#!/bin/ksh
# a file has a header record followed by detail records;
# variable text in positions 1-19 on all detail records must be changed to some constant text

# pattern to be replaced is obtained from record no. 2 positions 1-19:
source_file=$1
tmp_file=$source_file$$
oldtext="`sed -n -e '2p' ${source_file} | cut -c1-19`"

newtext="CHANGED TEXT"

echo $oldtext $newtext

sed '2,$ s/${oldtext}/${newtext}/g' ${source_file} > ${tmp_file}

mv $tmp_file $source_file
 
change single-quotes to double-quotes in your sed.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad,

That's fixed it.
However could you explain why the following single-quoted code is working:
old_user="048135"
new_user="048141"
sed '1,1 s/${old_user}/${new_user}/g' ${source_file} > ${tmp_file}


 
it should NOT - you may not have anything to substitute on the FIRST line.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top