M BUZZ CRAZE NEWS
// news

sed: Insert a string containing path delimeters

By John Parsons

I want to insert a line into a file at line 13 using sed. This is what I tried:

sed -i '13/home/me/dostuff.py &' /etc/rc.local

I get this error:

sed: -e expression #1, char 3: unknown command : `/'

How do I insert text containing slashes at a given line using sed?

1 Answer

Use

sed -i '13i\/home/me/dostuff.py &' /etc/rc.local

to insert it (i.e. place it before line 13); or

sed -i '13a\/home/me/dostuff.py &' /etc/rc.local

to append it (i.e. place it after the current line 13).


Note that forward slashes aren't special in sed's i and a commands - only in regular expression matches such as /pattern/d or s/pattern/replacement/ (although even there, they may be replaced by other delimiters such as \%pattern%d or s#pattern#replacement# - at least in GNU sed).

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy