What do sed and cut do?
Both read text line by line and print a changed version. sed, the stream editor, finds and replaces words, deletes lines and prints chosen lines. cut keeps only some columns from each line, which is handy for CSV files like the bakery’s menu.csv. A third tool, tr, swaps single characters, such as lowercase for uppercase.
None of them open a window. You describe the change in one line, and they apply it to every line of the file in one go. On a 6-line menu that saves a minute. On a 4,000-line order history or a folder of web pages it saves an afternoon, and you can repeat the exact same change next month.
By default all three print their result to the screen and leave the file untouched. That default makes them safe to experiment with. Only sed -i writes back into the file, and the next sections show when to use it.
How do you replace a word with sed?
The command s/old/new/ means substitute. Put it in single quotes, followed by the file name. Here is the bakery renaming its brown loaf, previewed on the screen:
$ sed 's/brown loaf/seeded rye/g' menu.csv
item,price,category
sourdough,4.50,bread
seeded rye,3.80,bread
cinnamon bun,2.20,sweet
fruit scone,1.90,sweet
seeded rye roll,0.90,bread

-i, sed prints the changed text and leaves menu.csv alone. Read this preview, then run the same line with -i to save it.What does the g at the end change?
Without g, sed replaces only the first match on each line and moves on. With g (global) it replaces every match on the line. On the menu each line mentions brown loaf once, so both forms give the same output, and that is exactly how the mistake slips through. A line such as Mill Ln counter (Mill Ln side door) has two matches, and only g reaches the second.
Two other flags you’ll see are i, which ignores case so s/brown loaf/seeded rye/gi also catches Brown loaf in specials.txt, and a number such as 2, which replaces only the second match. Most real jobs want g. Write it by default and drop it only when you mean the first match.
The single quotes protect the whole script from the shell. Without them, spaces split it into several words and characters like * or $ get expanded before sed sees them. If your replacement contains a slash, as in a web address, either escape it as \/ or pick another separator: s|/old-page|/new-page|g works the same way.
How do you save the change into the file?
Add -i, which stands for in place. sed -i 's/brown loaf/seeded rye/g' menu.csv prints nothing and rewrites menu.csv. The practice terminal adds a note to remind you there’s no undo, and a real terminal won’t even do that.
The safe routine is two steps. Run the command without -i and read the output. If it looks right, press the Up arrow, add -i after sed, and press Enter. The second run does exactly what the first one showed you. When the file is long, pipe the preview through grep to look only at the lines you care about, for example sed 's/brown loaf/seeded rye/g' menu.csv | grep rye.
Never try to save with sed 's/a/b/' menu.csv > menu.csv. The shell empties menu.csv before sed starts reading it, and you end up with an empty file. Write to a new name instead, or use -i.
On a Mac the built-in sed is the BSD version, and -i needs an argument for a backup suffix. Write sed -i '' 's/old/new/g' file there, or sed -i.bak 's/old/new/g' file to keep a copy called file.bak. The GNU sed on Linux servers accepts both -i and -i.bak, so -i.bak is the form that works everywhere.
How do you delete lines with sed?
Put a pattern between slashes and add d. sed '/scone/d' menu.csv prints every line except those containing scone. The pattern is a regular expression, the same kind grep uses, so /^#/d drops comment lines that start with # and /^$/d drops empty lines.
You can also delete by line number. sed 1d menu.csv removes the first line, which is how you strip a CSV header. sed 2,4d removes lines 2 to 4, and sed '$d' removes the last line. To print only certain lines, use -n with p: sed -n 2p menu.csv prints just sourdough,4.50,bread.
Deleting by pattern matches more than you might expect. /loaf/d would remove both the brown loaf and the brown loaf roll. Preview first, count the lines with | wc -l, and make the pattern tighter, for example /^brown loaf,/d, if it catches too much.
How do you pull one column out of a CSV with cut?
Tell cut what separates the fields with -d and which fields to keep with -f. Fields are numbered from 1. On the fixed menu, cut -d, -f1 menu.csv prints the item names:
$ cut -d, -f1 menu.csv
item
sourdough
seeded rye
cinnamon bun
seeded rye roll
$ cut -d, -f1,2 menu.csv | sed 1d
sourdough,4.50
seeded rye,3.80
cinnamon bun,2.20
seeded rye roll,0.90Where does cut go wrong?
Fields can be listed with commas, as in -f1,3, or as ranges, as in -f2-3. Without -d, cut splits on tab characters, so a CSV comes back unchanged and it looks as if nothing happened. -c cuts by character position instead: cut -c1-10 keeps the first ten characters of each line, which suits fixed-width logs.
cut is simple on purpose, and it has two limits. It can’t handle commas inside quoted fields, so a line like "loaf, large",4.50 splits in the wrong place. And it can’t treat several spaces as one separator, which matters for the output of ls -l. For quoted CSV use a spreadsheet or a short script. For space-padded text, awk '{print $2}' is the usual tool on a real computer, though the practice terminal doesn’t include awk.
What is tr for?
tr translates characters one for one. cat menu.csv | tr a-z A-Z prints the menu in capitals, and tr ',' '\t' turns commas into tabs. With -d it deletes characters instead, so tr -d '\r' removes the stray carriage returns that Windows files carry, a common cause of scripts failing with odd errors.
Unlike sed and cut, tr never takes a file name. Feed it from a pipe or with <, as in tr a-z A-Z < menu.csv. Writing tr a-z A-Z menu.csv fails. It says tr: extra operand 'menu.csv', here and in GNU tr. And tr works on single characters only, so tr loaf roll swaps l for r, o for o, a for l and f for l. Use sed whenever you want to replace a whole word.
What do AI-suggested sed commands get wrong?
Assistants often hand you sed -i straight away, with no preview, and sometimes run it across many files at once with find or a wildcard. Before you paste one, remove the -i and run it on a single file to see what changes. Check for the missing g, for a pattern that matches more than you intended, and for unescaped slashes in paths and web addresses.
Watch the Mac versus Linux difference too. A command written for macOS, sed -i '' 's/x/y/g', fails on a Linux server because GNU sed reads the empty quotes as the script. One written for Linux fails on a Mac the other way round. If the assistant doesn’t know where you’ll run it, tell it, or use -i.bak, which both accept.
Field notes
sed 's/old/new/g'replaces every match and prints the result.- Without
-i, sed only shows the change and leaves the file alone. sed '/word/d'drops each line that contains the word.cut -d, -f1keeps the first comma-separated column.
Easy to get wrong
Without g, only the first match on each line changes. A line that mentions the old address twice keeps the second one.
The shell empties menu.csv before sed reads it, so you save an empty file. Use -i, or write to a new name and check it.
Without -d,, cut looks for tabs, finds none and prints each whole line. Always name the separator for a CSV.
tr only reads from a pipe or <. A file name after the two sets is an error.
Questions people ask
Can I undo sed -i?
No. The old content is gone unless you have a copy. Use sed -i.bak to keep one, or commit the folder to git before big edits. Previewing without -i first prevents most mistakes.
How do I change text in many files at once?
sed -i 's/old/new/g' *.html edits every matching file in the folder. Run grep -l old *.html first to see which files will change, and try the command without -i on one of them.
Why does my replacement with a web address fail?
The slashes in the address end the s/// command early. Use a different separator, such as s|http://old|https://new|g, or put a backslash before each slash.
Should I use sed or a text editor?
For one change in one file, an editor such as nano is fine. Use sed when the same change repeats across many lines or files, or when you want to save the exact change to run again.