Intuition and ingenuity.

Mathematical reasoning may be regarded rather schematically as the exercise of a combination of two facilities, which we may call intuition and ingenuity. (Alan Turing)

LinuxBanner

Sed: Stream Editor

sed - stream editor for filtering and transforming text.
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s) and is consequently more efficient. But sed can filter text in a pipeline which particularly distinguishes it from other types of editors.
Sed is useful to swap, delete lines, add lines, replace lines... play with Arkanoid. Sed is too powerful to be explained in a short time , but some syntax for daily use can be described briefly.

Delete, in standard output, not directly on the file, all lines from the 3rd to the end

The output will be the first two lines of the files


sed '3,$d' testo1.txt # "$" stay for end of file, "d" stay for delete stefano,IT,unina marco,bio,unina


What is the difference between head -2 ???



sed -i '3,$d' testo1.txt # with the flagh "-i" we will work directly on the file, editing it. cat testo1.txt # display the file in order to check the result stefano,IT,unina # We've deleted all lines, except the 1st and the 2nd (from 3rd to EOF) marco,bio,unina # this operation is irreversible


Let's figure it out what would happen if...


awk 'NR>1' testo1.txt


And what with SED?
And what with "-i" flag?



sed -n '1!p' testo1.txt


Another example, this time to remove the last line on a file



sed '$ d' testo1.txt


Run again our script to restore file for our lesson


./creaFiles.sh


Use sed to replace a "," (or any other char or string) every 1st occurrence


sed 's/,/;/' testo1.txt stefano;IT,unina marco;bio,unina serena;bio,unina mario;IT,igb


Use sed to replace a "," (or any other char or string) globally


sed 's/,/;/g' testo1.txt # g stay for "global" stefano;IT;unina marco;bio;unina serena;bio;unina mario;IT;igb


Replace, only for biologists, the word "unina" with "MSA"

Use sed to replace a "," (or any other char or string) globally


grep bio testo1.txt | sed 's/unina/MSA/g' # we've used a grep to filter the word bio # So the standard output redirected with a pipe # to the command sed to replace "unina" with "MSA" marco,bio,MSA serena,bio,MSA


"Comment" with "#", every line of the file that contain "bio"


sed '/bio/s/^/#/g' testo1.txt # ^ means, write the new char at the beginnig of the line stefano,IT,unina #marco,bio,unina #serena,bio,unina mario,IT,igb


Delete every line that starts with the word "marco"


sed '/^marco/d' testo1.txt # ^ RegExp, d stay for "delete" stefano,IT,unina serena,bio,unina mario,IT,igb


Sed online official manual