1

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?

Porcupine
  • 1,680
  • 2
  • 19
  • 45
  • You're telling `sed` to substitute in a range that starts with a line that matches `# Start` and ends with a line that matches `$` and that's exactly what it does... – don_crissti Dec 11 '18 at 21:52
  • But how to tell that `$` means end of the file? (as in the referred link). [SED: insert text after the last line? - Unix & Linux Stack Exchange](https://unix.stackexchange.com/a/26639/220462) – Porcupine Dec 11 '18 at 21:53
  • _"I learnt that `$` 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... – don_crissti Dec 11 '18 at 22:00
  • @don_crissti Could you please give an example of how to use `$` 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. – Porcupine Dec 11 '18 at 22:01
  • If you go back to the page you linked to you'll see at least two examples of proper usage of `$` as an address. **Ignore the accepted answer there** which needlessly complicates things by double quoting and escaping the `$`. – don_crissti Dec 11 '18 at 22:04
  • @don_crissti I am clear with the idea that I am escaping `$` as I am using double quotes in the sed expression (for safer variable expansion). So, I used the exact thing here. Isn't it? Could you please find the flaw with my example? – Porcupine Dec 11 '18 at 22:09
  • No, you _don't need_ to escape the `$` in the RHS of that assignment. As to your problem, I recommend reading the manual if you really want to learn something... You'll find out that `$` is a special address that designates the last line _but not if you use it as a pattern_... – don_crissti Dec 11 '18 at 22:14
  • Imho, you are needlessly making a simple pbm complex by bringing in the vagaries of quoting into play. For understanding, you need to start simple. Once get a handle then add complexity nd see where it goes. – Rakesh Sharma Dec 12 '18 at 03:07
  • @RakeshSharma Could you please simplify this that works? I am asking you because I already tried this but could not do much. – Porcupine Dec 12 '18 at 10:41

1 Answers1

1

This works: (Note: ${Final} instead of /${Final}/ )

file=Data.txt
Initial="# Start";
orig="abc";
new="123"; 
Final='$' # Line Number
sed -r -e "/${Initial}/,${Final}s/${orig}/${new}/g" ${file} 
Porcupine
  • 1,680
  • 2
  • 19
  • 45