Ex 5 Study Drill with sed

Hi all,

I am using a mac for this.

I was giving a go at ‘Study Drills’ in ‘Exercise 5’ removing my_ and wanted to test out a function I learned when preparing for the book: sed.

My understanding is that I can substitute characters, delete lines, but I was not able to delete the characters ‘my_’ successfully and wanted to know the lazy way to delete them.

I tried running a shell script to remove ‘my_’:

echo "Enter file name using sed"

read fileName

if [[ -f $fileName ]]


  sed '/my_/d' $fileName


  echo "$fileName does not exist."


And this just printed the script out in my terminal, no changes. (WHY print it out?!)

I also attempted to run sed ‘/my_/d’ $fileName on the Terminal and that was weird as hell because it ended up printing just a couple of lines (WHY?!):

$ sed '/my_/d' ex5.txt

print(f"Actually not too heavy.")

# this line is tricky, try to get it exactly right

After looking up in a browser, videos, walked away for a bit… I just cannot figure this out.

Any guidance will be appreciated.

Thank you for your time.

We’d need to see the original script, but as for why it prints it: That’s what sed does if you don’t redirect its output. Try sed '/my_/d' script.py > new.py.

It probably deleted all the lines with my_ in them and printed the rest? The d command deletes the pattern space, not just the pattern. Try the s command.


That’s pretty advanced work there. I think @florian has a decent answer, but in Vim I just do it with:


Which is very similar to the sed command. Try it in vim.

1 Like

Thanks for the reply!

'/my_/d' script.py > new.py creates a document deleting all the lines where ‘my_’ is.

I tried the s command unsuccessfully because it looks like I have to substitute for something that occupies space, not deletes it, and a blank ’ ’ did not worked.

Thanks for helping out! I hope this is not too much of a mess, and I should move forward with the exercises. I might circle back around to this when I am finished with the book…

I tried vim, vim '%s/my_//g' ex5.my_remove.py > ex5.my_removed.py but two things are happening:

  1. The terminal presents:
    Vim: Warning: Output is not to a terminal

  2. ex5.my_removed.py contains:
    e[?1049he[>4;2me[?1he=e[?2004he[1;24re[?12he[?12le[22;2te[22;1te[27me[29me[me[He[2Je[?25le[24;1He[97me[41mE325: ATTENTIONe[m

    Found a swap file by the name “/var/tmp/g.swp”

    e[10Cowned by: XXX dated: Mon Aug 10 07:03:54 2020

    e[9Cfile name: %s/my_//g

    e[10Cmodified: YES

    e[9Cuser name: XXX host name: XXX-XXX-XXX.local

    e[8Cprocess ID: XXX

    While opening file “%s/my_//g”


    (1) Another program may be editing the same file. If this is the case,

     be careful not to end up with two different instances of the same
     file when making changes.  Quit, or continue with caution.

    (2) An edit session for this file crashed.

     If this is the case, use ":recover" or "vim -r %s/my_//g"
     to recover the changes (see ":help recovery").
     If you did this already, delete the swap file "/var/tmp/g.swp"
     to avoid this message.

    e[32mSwap file “/var/tmp/g.swp” already exists!e[m

    e[32m[O]pen Read-Only, (E)dit anyway, ®ecover, (D)elete it, (Q)uit, (A)bort: e[?25h

I also tried adding a blank between // vim '%s/my_/ /g' ex5.my_remove.py > ex5.my_removed.py

and I got this:

e[?1049he[>4;2me[?1he=e[?2004he[1;24re[?12he[?12le[22;2te[22;1te[27me[29me[me[He[2Je[?25le[24;1H"%s/my_/ /g" [New DIRECTORY]e[2;1He[94m~                                                                               e[3;1H~                                                                               e[4;1H~                                                                               e[5;1H~                                                                               e[6;1H~                                                                               e[7;1H~                                                                               e[8;1H~                                                                               e[9;1H~                                                                               e[10;1H~                                                                               e[11;1H~                                                                               e[12;1H~                                                                               e[13;1H~                                                                               e[14;1H~                                                                               e[15;1H~                                                                               e[16;1H~                                                                               e[17;1H~                                                                               e[18;1H~                                                                               e[19;1H~                                                                               e[20;1H~                                                                               e[21;1H~

I think what @zedshaw means is that you open the file in vim, execute the command on the open file and then save it.

vim ex5.my_remove.py

This should open the file in vim. Then type


and hit enter. All occurences of my_ should be deleted. And you can save the file with

:w newfile.py

But if you’re not familiar with vim anyway, I’d look for the search and replace command in your editor.

1 Like

I’m curious @dansmit, is there a special reason why you’re avoiding text editors and attempting to do these edits entirely from the commandline with sed?

To repeat concepts I have learned. I am doing things the long way where I can.