3

I'm trying to add a string 'rest_framework', after 'django.contrib.staticfiles', in a file, however when I do try something like

sed '/'django.contrib.staticfiles',/a 'rest_framework',' file

I get an error saying

sed: 1: "/'django.contrib.static ...": command a expects \ followed by text

I have tried the same command with \ around the symbols as well.

Answer: Installed gsed as base MacOS sed does not work well when creating lines under specified strings.

Vcoss
  • 95
  • 7
  • Possible duplicate of [How do I escape double and single quotes in SED?](https://stackoverflow.com/q/7517632/608639), [How to escape single quote in sed?](https://stackoverflow.com/q/24509214/608639), [How to correctly use quotes in sed?](https://unix.stackexchange.com/q/230351/56041), [How to escape a single quote?](https://unix.stackexchange.com/q/75988/56041), etc. – jww May 28 '18 at 17:08
  • This was most definitely not a quotes issue. @Inian solved it by instructing me to use gsed over sed. – Vcoss May 28 '18 at 18:20
  • Installing `gsed` isn't sufficient on its own to fix your problem. You *also* have to fix your command to protect single quotes from the shell so `sed` can see them. That part is certainly a duplicate. If you change your question to ask about MacOS `sed` breaking on a correctly-written command, then it would be a clear non-duplicate. – Peter Cordes May 29 '18 at 08:22
  • I had this since the begining `"/'django.contrib.staticfiles',/a 'rest_framework',"`, as can be seen with the error code. gsed fixed it by itself – Vcoss May 29 '18 at 12:51
  • Also stated that I had `\\` around them a testing still failed – Vcoss May 29 '18 at 13:25
  • Note that POSIX [`sed`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html) requires ```a\``` and the text on the next line or lines, with a backslash indicating a continuation. BSD (macOS) `sed` follows the standard. GNU `sed` changes the rules and allows the first line of appended data on the same line as the `a` command. GNU `sed` is the aberrant program — useful though its aberrance is. If you want to write code that will port between platforms, you follow the POSIX standard and not GNU's alternative standard. If you must use GNU's notation, install GNU `sed`. – Jonathan Leffler May 29 '18 at 20:19

3 Answers3

4

Using single quotes inside one another gets real messy. Use double-quote to preserve the literal value of the search string. As long as you don't have search/replace string containing $ strings, using double-quotes should be fine

sed "/'django.contrib.staticfiles',/a 'rest_framework'," file

Apparently the FreeBSD version of the sed on MacOS was still throwing errors for the above syntax and using GNU sed by installing brew install gnu-sed and running the command with gsed worked fine.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • See also my [comment](https://stackoverflow.com/questions/50531041/how-to-add-another-string-in-the-next-line-to-a-specific-string-on-macos-sed#comment88193962_50531041) to the main question. – Jonathan Leffler May 29 '18 at 20:20
2

to add 'rest_framework', after 'django.contrib.staticfiles',:

$ echo \'django\.contrib\.staticfiles\'\, | 
  sed  "s/\('django.contrib.staticfiles',\)/\1'rest_framework',/g"
'django.contrib.staticfiles','rest_framework',

To get the 'rest_framework', on the next line, add \n after \1.

James Brown
  • 36,089
  • 7
  • 43
  • 59
1

Following awk may help you here too.

awk '{gsub(/\047django.contrib.staticfiles\047\,/,"&\047" ORS "rest_framework\047,")} 1' Input_file

In case of saving output into Input_file append > temp_file && mv temp_file Input_file in above code.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93