How to use sed to automate big code changes (GUIs hate this trick)

Поделиться
HTML-код
  • Опубликовано: 26 июн 2023
  • sed is a great command line tool for making code changes to codebases of any size. In this video, we'll look at several tips for quickly getting up and running with safely making these types of changes.
    The General Problem: xkcd.com/974/
    My Links
    shaky.sh
    shaky.sh/tools
    / andrew8088
    mastodon.social/@andrew8088
    #terminal #commandline
  • НаукаНаука

Комментарии • 54

  • @mettle_x
    @mettle_x Год назад +36

    3:50 Correction: It's called Forward Slash (/). Backslash is \

  • @swapnilbhattacharjee183
    @swapnilbhattacharjee183 Год назад +1

    Great video Andrew 👏

  • @Fullflexno
    @Fullflexno Год назад

    Great video!!🎉

  • @nexxel
    @nexxel Год назад

    great vid Andrew!

  • @richardblain4783
    @richardblain4783 Год назад +1

    You don’t need to find the exact files that you want sed to process. sed -i does nothing to input files that have no matching lines.
    I typically run sed with -n, which removes all non-printed lines from its output, and the “p” modifier, which prints the changed lines, on all of the “s” commands (yes, I put several “s” commands in one sed command), and then pass all of the files, or all the C/C++ source and header files, in the directory to my sed command. The output is a dry run that shows me what sed will change. When I’m satisfied with the changes, I remove the “p” modifiers and change the -n option to -i.
    Bonus tip: If you run sed -i, but didn’t remove all of the “p” modifiers, sed will edit the matching lines in place and print them, resulting in duplicate lines in your files. The uniq command can be used to remove the duplicates.

  • @bart2019
    @bart2019 Год назад

    You should be careful that you don't replace more than you wanted, in particular you should take care that you only match "I" as a complete word. In your example that can be done by putting "\

  • @rendezone
    @rendezone Год назад

    Amazing description of sed substitutions. The extra arg in sed -i always drives me nuts in scripts that run on OSX and CI Linux

  • @Luxcium
    @Luxcium Год назад

    OK what I would love to have in a future video, is our beloved content creator... I would love to have kind of a biographical time line but of the learning process, like at first someone barley know the existence of a computer and then discovers its passion for something in particular, I would like to understand the order in which he learned his skills and art... especially every think that is accessory to the main languages and technologies and stacks... Type touch, vim or Neovim, git and git hub (or maybe he started using GitLab or what not) and bash scripting (this is something that I have learned and I am always curious about how normal human being are learning that kind of cursed language), I am using linux so I would have a lot to say about my learning curve of the OS, personally I was unable to use Windows anymore after owning a macBook Pro for almost 10 years and not having enough money to get a new one while having an expensive desktop computer made me chose Fedora but having a macOS based system is amazing and maybe te prodigious and modest superhero would be interested in telling us if he started with Windows or since when he is using macOS and what not... for my part then listing all the main stack(s) libraries and programming language he learned would be enough but maybe for other it would be worth explaining in as much detail the time line for that... I am very interested in all that's not the main chunk of the stack because all those other accessory elements that are super important in fact are always forgotten and never shine in any stories about the thing one might know, love and use... _(so future me told me not to use ChatGPT to fix any typos because it would end up taking 90 minutes and I would not be able to get the message I really wanted)_

  • @JoshuaGutz
    @JoshuaGutz Год назад

    I'm new to bash. I know this wasn't the point of the video but wanted to share what I'll be using moving forward. I've been doing this, "for file in $(grep -l ) ; do ; done; " . Very cool to see the cleaner "grep -l | while read file; do ; done; "
    Also, the tip of leaving the echo in place to see the command that will run is awesome. I'm going to use that. One last thing when I'm doing some for (now while) loop like this in bash command, I'll add a " | head" in some place so my output doesn't fill my terminal, and remove it when I'm ready to run the full thing.

  • @driden1987
    @driden1987 Год назад +2

    This is a great video, coreutils are awesome and every dev should get familiar with them

  • @nagyszabolcs9451
    @nagyszabolcs9451 Год назад

    Great video! I never would have thought of using sed in such a situation 😅
    One note is that you have to make sure that you're running the command from a src folder, as to exclude node_modules, documentation and whatnot.

    • @evyatarshafran5017
      @evyatarshafran5017 Год назад +1

      Actually, most grep-like commands follow your gitignore so node_modules would probably not be a problem

  • @slavkaeroxin9927
    @slavkaeroxin9927 Год назад

    OR you can use vim's quickfix list, with extra superiority over gui boys too:
    `:grep -r /path` (or otherwise populate quickfix list, e.g. in Telescope)
    (optional) `:copen`
    `:cdo ` do any command on matches, e.g. `:cdo s/pattern/replacement/`, `:cdo norm! }P` (go to next paragraph, paste before the cursor)
    `:cdo update` (write modified files)

    • @andrew-burgess
      @andrew-burgess  Год назад

      I've been putting off learning how to use the quickfix list, but I really need to get into it.

  • @MarshallLevin
    @MarshallLevin Год назад +1

    You might want to consider a perl one-liner in place of sed. You can get the same in-place behavior and you can easily work with multiline regexes.

    • @andrew-burgess
      @andrew-burgess  Год назад

      Yeah! I've done just a little bit with perl, and it's scary powerful!

  • @nikolica9999
    @nikolica9999 Год назад

    awesome!

  • @FolkOverplay
    @FolkOverplay Год назад +1

    Cool way of doing it! But this seems to be breaking imports, no? I also have not seen an editor that can’t do this built in and even update imports etc. at the same time. Is this ever better than a standard editor?

  • @mikefochtman7164
    @mikefochtman7164 10 месяцев назад

    Main reason I learned 'sed'?? So I could use 'awk' a lot better. :) Really, once you've got 'sed' down pat, 'awk' is super. Over the years, I've done so many code imports and refactors using awk.

  • @amans6504
    @amans6504 Год назад

    Crazy just this week i was working on a bash script, wanted a basic PascalCase util with sed but macOs sed is just different and output some ugly characters like "U" for no reason

  •  4 месяца назад

    / is actually a forward slash ;)

  • @jenselstner5527
    @jenselstner5527 Год назад

    That's when I started to learn Perl. So I didn't have to learn bash, sed and awk. ;o)

  • @rcarias78
    @rcarias78 11 месяцев назад

    how do you do the same using MACOS sed

  • @OcteractSG
    @OcteractSG Год назад

    I used sed to modify playlist files (.xapf) so that I could use them on other devices. It’s really handy.

  • @Simplicity4711
    @Simplicity4711 Год назад +1

    That slash is a forward slash. That one is the back slash: \

  • @krzysztofzabawa2212
    @krzysztofzabawa2212 Год назад +4

    I really like your content Andrew, (I do!) however this seems to be an awfully hard and error prone way to replace built-in editor functions such as 'rename' or 'search and replace'. In my editors can also be supplemented with regular expressions to be even more powerful. Learning completely new set of tools just to mimic built-in editor function seems... like overdoing it a bit ;)

    • @fagnersales532
      @fagnersales532 Год назад

      That's what I thought initially, but probably there are people around the world that doesn't use "fancy IDEs" or just love doing everything in the terminal, specially those who use vim, it ends up being easier for them.
      Edit: Sometimes you won't have an IDE to help you out, it happened a lot for me and knowing those type of command would be very handy at the time

    • @mr.vladislav5746
      @mr.vladislav5746 Год назад

      @@fagnersales532 Can you give an example of when you won't have an IDE to help?

    • @fagnersales532
      @fagnersales532 Год назад

      @@mr.vladislav5746 When I connect to a computer through a terminal.

    • @apasserby9183
      @apasserby9183 Год назад

      new set of tools? regex is regex (usually)

    • @richardblain4783
      @richardblain4783 Год назад

      I use vim and sed extensively. vim’s “s” command is fine for changing patterns within single lines, and the “g” command (executing a “d”, “t” or “m” command) for adding or removing lines around patterns. But nothing beats sed for finding and changing patterns spread across two or more consecutive lines. I also use a code formatter, which breaks lines at unpredictable points, so I have to find and replace multi-line patterns often.

  • @fahadahaf
    @fahadahaf Год назад +16

    Or just use a good editor that can find and replace all over the project, even vim can do that quite easily. coreutils are great, but there are much better use cases to demonstrate their power.

    • @TimothyWhiteheadzm
      @TimothyWhiteheadzm Год назад

      My thought exactly. If you are simply doing a search and replace any IDE/editor can do that with ease. I was hoping for something that could do more complex code manipulation but this clearly isn't it. Just an overly complex way to to basic search/replace.

    • @bierrollerful
      @bierrollerful 11 месяцев назад +1

      The point of sed is that it a _non-interactive_ editor. If you want to write a script that updates the version number of your project you will use something like sed.
      Obviously, you could also do that by hand, using an _interactive_ editor, but why would you if you could just use a script?

    • @TimothyWhiteheadzm
      @TimothyWhiteheadzm 11 месяцев назад

      @@bierrollerful There is nothing 'interactive' about normal search and replace in a code editor. It is no more or less interactive than a script or command line tool. It IS easier. Search and replace by command line is stupid.

    • @bierrollerful
      @bierrollerful 11 месяцев назад +2

      @@TimothyWhiteheadzm Non-interactive means that it does not require further user input.
      Imagine you want to make the work of finding and replacing some text repeatable. For example replacing the version number of your project, which might be in multiple places all over your project.
      With an IDE, you would note down the steps (or the _interactions_ you have with the editor) you need to do. Then, whenever you need to do the same work again, you retrace those steps, interacting with the IDE in the same way as you did before.
      Or you can do the same work from the command line. You'd use grep to find all the files with the version number, and you'd then use sed to replace that version number with the new one. This is all done just with function arguments and entirely without user prompts.
      Why bother? *Because you can reuse those commands in a script* (or makefile). Doing the same work again then becomes a matter of milliseconds. You can also have that work be automated (f.ex. a github hook). But most importantly: *anyone can reproduce the exact same work without fail by simply running the script.* If you work on a project with multiple people, scripts (and thereby using command line utils like sed) become mandatory, because people are terrible at following manual instructions.

    • @tompov227
      @tompov227 11 месяцев назад +1

      I actually think this is a good tutorial because a lot of the time you don’t have fancy editors and especially custom, configurations and remote machines so being able to do something this complicated with something as simple as sed is actually pretty handy

  • @samuelgunter
    @samuelgunter Год назад +1

    3:50 that's a forward slash /, not a back slash \, doesn't matter much tho

  • @Luxcium
    @Luxcium Год назад

    But there is only one Monday per week and only one 8 am EST/EDT per Monday every other time slots is a *never*

    • @andrew-burgess
      @andrew-burgess  Год назад +1

      Lol, I'm switching it up a bit, thanks for noticing :) need to update that banner

    • @Luxcium
      @Luxcium Год назад +1

      @@andrew-burgess I was complaining that it was only once per week and would have not being using such a sarcastic tone if it was not because I was implying a compliment ― that is that I would love tho have more content implies that I love it... but to be hones I do love the presentation and the presenter also... Edit: I am not « in love with » but I meant to say I do appreciate a lot the beloved content creator _(I did't wanted to sound like anyone was exposed to Pink Kryptonite here) ..._

  • @moneyfr
    @moneyfr Год назад

    You could use sd from rust

    • @dmitriidemenev5258
      @dmitriidemenev5258 Год назад

      Thanks for telling about this tool. It looks pretty good.

  • @erikslorenz
    @erikslorenz Год назад

    Nice title lol

  • @gvarph7212
    @gvarph7212 Год назад +4

    What editors are you using that don't have a findAll/replaceAll functions?

  • @jenselstner5527
    @jenselstner5527 Год назад

    My solution would be similar to yours:
    perl -i -pe 's/I/AsyncIterable/g' $(find -type f)
    -p would be the implicit while loop with automatic "printing" the changed lines into the file.

    • @andrew-burgess
      @andrew-burgess  Год назад

      Love this! I recently learned about "Perl pies", so cool!