From SED: insert text after the last line? - Unix & Linux Stack Exchange, I learnt that $ if used outside the substitution in sed represents the end of file. This I have tried to use in the follwing to replace all occurences of abc occuring after # Start and till end of the file by 123
Code:
file=Data.txt
Initial="# Start";
Final='\$'
orig="abc";
new="123";
sed -i -r -e "\!${Initial}!,\!${Final}!{s!${orig}!${new}!g}" ${file} ;
Data.txt
# Start
abc
abc
$
abc
abc
Output:
# Start
123
123
$
abc
abc
Expected Output:
# Start
123
123
$
123
123
What am I missing? i.e. How to tell to sed that $ means end of the file?
Note: I am escaping $, as I am using double quotes in the sed expression (for safer variable expansion). I hope this is correct. Can you comment on this?
sedto substitute in a range that starts with a line that matches# Startand ends with a line that matches$and that's exactly what it does...$means end of the file? (as in the referred link). SED: insert text after the last line? - Unix & Linux Stack Exchange$if used outside the substitution in sed represents the end of file" ... Well, not quite, apparently... What's stopping you from using$? And btw, of all delimiters that could be used,!is probably the worst choice...$in above example to mean end of file? Also if possible, please give a suitable delimiter that you think is usually good in most of the cases. I used!as it is rarely used symbol.$as an address. Ignore the accepted answer there which needlessly complicates things by double quoting and escaping the$.