That's What She `sed`
Hey guys! Today, as you might've guessed, we're gonna be talking about the sed command! Sed is (as defined by the manpage) a "stream editor for filtering and transforming text." This is a fancy way to say that you can edit the text of a file (as well as standard input, of course) via a terminal command rather than loading the entire file into memory with a text editor. For example, if you had to switch pizza night to taco night all month long, you could use `sed s/pizza/tacos/g menu.md > newmenu.md` to instantly create a new menu reflecting these changes globally instead of having to manually go into the file and edit it (this substitution command should look familiar to any Vim users out there.) Why is this useful? If I need to change the contents of a file, why not just use a text editor? Scripting, first and foremost, as being able to change a files contents without editing it is a powerful skill (and more memory efficient!) Besides the substitution command, which is sed's primary function, there are also 25 other commands that can manipulate text. The rabbithole goes pretty deep, as this is a Turing-complete language and, like awk, people have built whole programs using sed (check out https://github.com/moldabekov/chess-sed for a peak into the madness) so the limits are your imagination and lack of desire to touch grass really. Among these commands are delete, which is used with the `d` key and can do a whole lot of just that. Some examples include `sed '69,420d' foo.txt` to delete lines 69 and 420 of a file and `sed '/yoloswag/d' bar.txt` to delete all occurrences of a word in a file. The next one on the list is print and you're probably like "but Steven, I can already use grep to do that" Shut up, I don't care, this article is about sed so we're gonna do it with sed, k? Basically `sed -n '/FAIL/p' tas.txt` will give you the same output that `grep 'FAIL' tas.txt` would. Next up is inserting and appending! You wrote a script, and went to run it but you forgot your shebang and you're too lazy to open your editor back up (bad example but follow me here), you can just use `sed -i '1i\#!/bin/bash' script.sh` to throw that baby in and you're good to go. Moving on, we've got change, which is used with the `c` key. This ones pretty straightforward, `sed '42c\Ka is a Wheel' foo.txt` would change line 42 of the example file. You can also use sed in place of head or tail if you wanted to. `sed '10q sedarticle.md` prints the first 10 lines of a file and `sed -n '5,$p' sqlarticle.md` prints lines 5 through the end of the file. There's about a million other things that you can do with sed and we've definitely only grazed the surface of the iceburg but its primary use is definitely substitution, which includes being able to substitute specific ranges of lines in files as well (I know, its dope as hell), which we can do pretty dang easily: `sed '42,69s/foo/bar/`. As you can see, sed is awesome, and an absolutely necessary tool in the world of Bash scripting, and really and Linux-based programming in general as far as I'm concerned. That's it for today! Check out my other most recent article "Getting My Benedict C***B*** on with SQL Murder Mystery" more more wacky edu-tainment fun! Peace!