1

This question already has an answer here:

Would like to append path variables to end of line on a specific row with search pattern and append, "Defaults secure_path" string I want to append: :/etc/bla/foo/

the file,

filename

# asdf
# lkaj
# asdah

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## safdafdhdahdabfdabf
## Aedhadhfdahdahadhda

Tried pattern search for "Defaults secure_path" and append to end of line:

sed '/^Defaults    secure_path = / s/$/ :/etc/bla/foo/' filename

But, got error:

sed -e expression #1, char 38: unknown option to `s'

marked as duplicate by don_crissti, αғsнιη, Bananguin, Kusalananda, roaima May 22 '18 at 20:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • but sed never saved the file when editing with vim it never stuck. – johnsnow May 22 '18 at 9:55
  • 1
    You were almost there: 1) By default, sed outputs the stream to stdout. If you want to edit in place, check whether your sed version has the -i option to directly edit the file. 2) If your pattern contains a slash, use a different delimiter for the s command, for example: s#$# :/etc/bla/foo# 2.5) Is the whitespace near the colon intentionally? – Philippos May 22 '18 at 11:14
  • Are you trying to edit /etc/sudoers with sed? I really wouldn't do that - it's way way too easy to break the configuration - and therefore the command - if you get it even slightly wrong. Use the interactive visudo instead. – roaima May 22 '18 at 20:17
0

you can use awk command and append it.

bash-4.1$ cat filename
# asdf
# lkaj
# asdah

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## safdafdhdahdabfdabf
## Aedhadhfdahdahadhda
bash-4.1$
bash-4.1$ awk '$0~/Defaults/{$0=$0":/etc/bla/foo/"}1' filename
# asdf
# lkaj
# asdah

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/etc/bla/foo/

## safdafdhdahdabfdabf
## Aedhadhfdahdahadhda

this awk command search for the pattern "Defaults" and append the value at end of the line.

using sed command

bash-4.1$ sed "s?\(Defaults.*\)?\1:/etc/bla/foo/?" filename
# asdf
# lkaj
# asdah

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/etc/bla/foo/

## safdafdhdahdabfdabf
## Aedhadhfdahdahadhda
  • found out that sed -i option saves the text – johnsnow May 22 '18 at 13:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.