0

im trying to edit big textfiles with sed in one command

what i try to do is to execute sed and replace a certain string with the same string and added newlines+strings, so i can add text.

example command:

sed -i "s/openssl_verify_mode: 'none'/openssl_verify_mode: 'none'\n&  domain: 'test.net'\n&   user_name: 'admin'\n&   password: 'mypassword'/g" /var/www/test-pro/config/configuration.yml

i use \n& to get the newlines. what am i doing wrong/what do i have to write?

  • 1
    Only GNU `sed` understands `\n`; and what do you use `&` for? – Fravadona Jun 27 '22 at 10:25
  • @Fravadona : I guess this is also a gnu-sed feature. See the accepted answer to [this](https://stackoverflow.com/questions/35252285/insert-text-with-sed-on-the-same-line-that-pattern-search) question. But in the context of the OP, it does not make much sense, because the matched text is also repeated verbatim. – user1934428 Jun 27 '22 at 10:49
  • [edit] your question to show a [mcve] with concise, testable sample input and expected output so we can help you. Also tell us which sed version you use (if it's not GNU sed then your use of `\n` and `-i` are wrong). Regarding `what am i doing wrong` - you'd have to tell us what your code is doing (wrong output, no output, syntax errors, something else) that you don't expect it to do for us to be able to answer that. – Ed Morton Jun 27 '22 at 11:44
  • 1
    @user1934428 `&` to mean "the text that matched the regexp" is standard in all seds. – Ed Morton Jun 27 '22 at 11:45
  • Instead of "replace a certain string with the same string and added newlines", you ought to think of it as "append a line after a certain string". Sed has a command for that. – William Pursell Jun 27 '22 at 11:48

1 Answers1

1

sed has an append command that you can you use to add text after a match:

sed -e "/openssl_verify_mode: 'none'/a\\
openssl_verify_mode:  domain: 'test.net'\\
openssl_verify_mode:  user_name: 'admin'\\
openssl_verify_mode:  password: 'mypassword'\\
" input-file

But it would probably be much cleaner to add the new text to a file and use the read command:

$ cat > new-text << EOF
openssl_verify_mode:  domain: 'test.net'
openssl_verify_mode:  user_name: 'admin'
openssl_verify_mode:  password: 'mypassword'
EOF
$ sed -e "/openssl_verify_mode: 'none'/rnew-text" input

If you want to parameterize the string "openssl_verify_mode" (ie, if you want to use a different string), do it at the shell level. eg:

$ s="openssl_verify_mode"
$ sed -e "/${s}: 'none'/a\\
$s:  domain: 'test.net'\\
$s:  user_name: 'admin'\\
$s:  password: 'mypassword'\\
" input

I strongly recommend against -i, and have omitted it from the answer. Feel free to abuse it if you like.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • With GNU `sed` you could also use `sed "/openssl_verify_mode: 'none'/a\ domain: 'test.net'\n user_name: 'admin'\n password: 'mypassword'/g" `, though I like the readability of yours better... but is the original string repition needed? I thought that was just an artifact of his `&` (though I'm not familiar with the format of the file.) – Paul Hodges Jun 27 '22 at 16:27
  • 1
    @PaulHodges I assumed the OP wanted those headers, but using `&` would also carry along the `: 'none'` so I don't really know what the OP actually wants. My proposed output is pure speculation. – William Pursell Jun 27 '22 at 18:42