1

I want the sed equivalent of this line of vim:

:s/","/",^M"/g

The goal: Take a line that contains a massive json block, and break it up into individual lines.

An example of what I want to work with is http://www.cartoonnetwork.com/video/teen-titans-go/episodes/index.html (look for function getFullEpisodes()).

What I tried:

519  sed -e 's/","/",\n"/g' < /tmp/url > /tmp/line-url

-- did not work, just put "n" at the proper place.

521  sed -e 's/","/",\\n"/g' < /tmp/url > /tmp/line-url

-- did not work, put a backslash followed by n at the proper place.

The full "script" of vim commands that I want to turn into a shell script:

:s/","/",^M"/g
:1,$!grep seo
:g,^.*/vid,s,,http://www.cartoonnetwork.com/vid
:g/"},{.*/s///
:$s,"}];,,
:wn
5
  • 4
    Are you just trying to print the json out niceley? You can do that without sed: cat foo.json | python -m json.tool or cat foo.json | jq '.' Aug 3, 2017 at 15:28
  • Could you explain what those esoteric vim commands are supposed to do for those of us who use the one true editor?
    – terdon
    Aug 3, 2017 at 15:44
  • @terdon There is no one true editor with more than three letters in its name
    – xhienne
    Aug 3, 2017 at 15:49
  • The input files are playlist files from Cartoon Network. The lines do the following: 1, break the json into parts; 2, look for the lines that contain 'seoFriendlyUrl' (and I need to use the full name, another playlist file now has 'seo' in the middle of another line where two words meet); 3, fix up the front of the resulting lines to look like normal URL's; 4 and 5, fix up the tail ends (the last line has a different end); 6, go to the next file so I can repeat these commands.
    – Keybounce
    Aug 3, 2017 at 15:58
  • So the output line that I want to break starts with ' return [{"title":"Infamous",' ... and that python tool doesn't like that start. Close though ... (NB: There's 2 tabs in front that get trimmed here.)
    – Keybounce
    Aug 3, 2017 at 16:04

1 Answer 1

3

It seems your version of sed does not understand \n on the RHS of s///.

You could always do this on a bash command line:

sed -e 's/","/",\
"/g' < /tmp/url > /tmp/line-url

On a csh command line, you would need to double the backslashes. Note that the trailing backslashes should not be followed by anything other than a hard newline.

5
  • That works! Thanks. Now, do you know how to make that "portable" enough that it can be turned into a script that will work for any version of sed? My sed's man page actually states that a hard newline will not work, but the truth is, it does (man page and program do not agree.)
    – Keybounce
    Aug 3, 2017 at 15:28
  • @Keybounce Actually a hard newline will always work. The \n may or may not. What version of sed are you using and on what OS?
    – user218374
    Aug 3, 2017 at 15:32
  • Mac OS 10.9.5. The version of sed is the version supplied with the OS (--version does not work). Now, for the next annoyance, putting two conext addresses in front of the substitute command does not limit it. $ curl -s -S $url | sed -e '/getFullEpisodes/,/return/ s/","/",\ "/g ' | grep seo > /tmp/url-line2 (hope that pasted right) does not restrict it to the two lines, starting at "getFullEpisodes" and ending with the return on the next line. Any line with "return" is included.
    – Keybounce
    Aug 3, 2017 at 15:49
  • @Keybounce please remember to always mention your OS. The implementation of the various utilities can be very different across different unices. OSX tends to use the BSD versions, for instance and those are often different to the GNU ones used by Linux systems.
    – terdon
    Aug 3, 2017 at 15:50
  • @Keybounce Use the -n option of sed and change that /g => /gp
    – user218374
    Aug 3, 2017 at 16:15

Your Answer

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

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