2
sed -i -e 's_.*_text-to-be-insterted-on-each-line&_' '/media/root/<filename>' 

works well and is concise and clean like i prefer.....but i dont / cant find ANY information about the ninja commands used - not in manual nor google.

  • sed = Stream EDitor
  • -i = in-place (i.e. save back to the original file)
  • The command string:
    • s = the substitute command

but im having trouble understanding from example how _.*_ is translated

and how &_ is being used exactly &_ is being used

if someone could explain GREAT or any resources that document these unique sed commands?

alex
  • 21
  • 1
  • 1
    The character used right after the first `s` is used as the *delimiter* for the subsequent regex expressions. That way, you can use slash (/) in those expressions without escaping. So to replace slash (/) with 'x', you could write, `s_/_x_g`. You could also write `s:/:x:g` which uses `:` as the delimiter. See [How to replace strings containing slashes with `sed`](https://stackoverflow.com/questions/16790793/how-to-replace-strings-containing-slashes-with-sed/16790859#16790859). – lurker Jan 24 '18 at 13:24
  • thanks good explanations of delimiter - still unclear on * but - likely need to do more reading – alex Jan 26 '18 at 01:16
  • `*` means zero or more of the previous regex or character (if it's just a character)`. A dot `.` matches any character. So `.*` means zero or more of any character. – lurker Jan 26 '18 at 02:55

1 Answers1

2
s_.*_foo&_

is same as:

s/.*/foo&/

You can use other separators as well, for example:

s#.*#foo&# or s@patter@replacement@

Some time it is convenient to use separator other than / in s substitution. For example, you want to do some changes on file path.

The & in replacement part, means the matched group 0 with the given pattern (regex).

An example may better explain:

kent$  sed 's/aa/*&*/'<<<'aabbcc'
*aa*bbcc

You have seen that the matched group 0 is aa in above input string.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • still not sure about original command but your brief example was helpful in explaining delimiters – alex Jan 26 '18 at 01:18
  • you should know some regex to understand the `group and &` . in short, `&` is what matched your given pattern. @alex – Kent Jan 26 '18 at 09:32