How do i use basic level sed?
-
Sed commands on a file, run on the entire file by default , unlike ed for instance which works on just the current line by default.
Unlike the ed command, Sed does not edit the file you give it, the output has to be piped or redirected to a file.
I have a test file called data_file with contents
hamster, furry little creature, 2 hamster drive donkey, back breaking helper, 8 donkey jacket road horse, top racer, 12 Horse throat Lane badger, night rider, 15 Badger Close cow, love eating grass all day, 16 Moofield Aveune sheep, can you take this coat off, 6 Wool road
Chain two sed commands together using semicolons, first change drive to gardens and second change Close to close.
$ sed 's/drive/gardens/; s/Close/close/' data_file hamster, furry little creature, 2 hamster gardens donkey, back breaking helper, 8 donkey jacket road horse, top racer, 12 Horse throat Lane badger, night rider, 15 Badger close cow, love eating grass all day, 16 Moofield Aveune sheep, can you take this coat off, 6 Wool road
Another way to chain sed commands together with "-e" option of sed
$ sed -e 's/drive/gardens/' -e 's/Close/close/' data_file hamster, furry little creature, 2 hamster gardens donkey, back breaking helper, 8 donkey jacket road horse, top racer, 12 Horse throat Lane badger, night rider, 15 Badger close cow, love eating grass all day, 16 Moofield Aveune sheep, can you take this coat off, 6 Wool road
You can add a number sed commands in a sed script and call it with the "-f" option.
sed -f scriptfile fileThis is a useful sed command, "-n" do not print the output by default, but add "p" to the end of the command, this way you can see only the line that are changed.
$ sed -n -e 's/drive/gardens/p' data_file hamster, furry little creature, 2 hamster gardens
© Lightnetics 2024