sed: one stream editor to rule them all
Sed
I enjoy UNIX, a lot. And A few weeks ago I was reading a book about unix when I
stumbled upon one of the oldest editors in unix and that is ed
. The more I
looked into it the more I realised where a lot of vim featurs come from.
- well,
sed
is very much the stream version ofed
.
Nice!
Small list of stuff that sed does:
sed
will replace in the stream the same way vim does by usings\
.
Examples:
I was looking at converting some simple latex files into markdown. I know pandoc would have done the job but these were pretty simple, and some of them had some errors in them that I did not want to fix. So I sed this :
sed "s\[\]end{document}\ \g" rprt2-methodology.tex | \
sed '1,29d' |\
sed "s\[\]end{enumerate}\ \g" |\
sed "s\[\]section{\# \g" |\
sed "s\[\]subsection{\## \g" |\
sed "s\}\ \g"
sed "s\[\]section\# \g" rprt1-bugs-and-omissions.tex \
|sed "s\[\]item \- \g" \
|sed "s\[\]begin{compactitem}\ \g" \
|sed "s\[\]end{compactitem}\ \g" \
|sed "/end/ d" \
|sed "/^[ ]*$/ d" \
|sed "/^$/ d" \
|sed "1,13 d" \
|sed "8,12 d" \
|sed "s\[\]verb\ \g" \
|head -n 50
Ok, it might seem like a lot, but how did I develop this? By simply running each
until I had the result I wanted. To repeat and improve i used fc
, this way I
would run them one by one, and then improve until I got the result I wanted.
Command | Description |
---|---|
ā\s\foo\bar\gā | just like in vim substitutes globally |
ā1,50dā | delete from line 1 to line 50 |