sed

A few sed commands that are useful and that I'd like to remember.

Get a sane list of the $PATH:


echo "$PATH" | sed 's/:/\n/g'
    

See what directories in $HOME are in $PATH:


echo "$PATH" | sed 's/:/\n/g' | grep "$HOME"
    

Compare different lines from two (or more even) different files:


cat <(sed -n '7,10p' file1) <(sed -n '1,3p' file2)
    

Copy matching lines from a file if using Wayland. Alternatively, replace the regex with line numbers:


sed -n '/regex/p' file.md | wl-copy
    

Replace a specific occurence in a line, like a field replacement:


cat fruit.dat
apple apple apple apple

sed 's/apple/orange 2' fruit.dat
apple orange apple apple
    

Or, specify a specific line, like line 1:


cat fruit.dat

apple apple apple apple
apple apple apple apple
apple apple apple apple
apple apple apple apple

sed '2s/apple/orange 2' fruit.dat

apple apple apple apple
apple orange apple apple
apple apple apple apple
apple apple apple apple