
Sed – An Introduction and Tutorial - aethertap
http://www.grymoire.com/Unix/sed.html
======
narrator
I just use | perl -pi -e 's/foo/bar/g' , etc for this kind of stuff. Is there
anything I can't do with perl on a line that sed will do? I can see how perl
is a lot more complex than sed, but I went through the whole perl learning
curve back in the late 90's so it doesn't bother me that much..

~~~
scdlbx
It's a lot easier to delete specific lines using sed. Also you can have sed do
replacements to the n'th instance of something. Doing that in Perl is a bit
more complicated and a lot less succint.

$ echo "foo foo foo foo" | sed 's/foo/bar/3'

foo foo bar foo

~~~
raiph
The Rakudo Perl 6 compiler is still immature and slow, and the -i option (in-
place edit) hasn't yet been implemented, but, at least for comparison's sake:

$ perl6 -pe 'next if ++$ == 2' example.txt

... prints all lines except line 2.

This is an example from Perl 6 One Liners[1].

The `$` is just just an unnamed variable that is getting incremented once per
evaluation (-e is for `evaluate`) which in this case happens once per line (-p
is for printing each line of input after eval'ing the code -- unless a `next`
applies, in which case that line gets skipped).

And...

$ echo "foo foo foo foo" | perl6 -pe 's:3rd/foo/bar/'

... replaces the third foo with bar.

P6 regexes are far easier to read and way more powerful than P5 regexes. The
`:3rd` bit is a general language feature called "Adverbs", in this case
applied to the regex focused s/// built in.[2]

[1] [https://github.com/sillymoose/Perl6-One-
Liners](https://github.com/sillymoose/Perl6-One-Liners)

[2]
[http://doc.perl6.org/language/regexes#Adverbs](http://doc.perl6.org/language/regexes#Adverbs)

~~~
cogburnd02
I've read literally nothing about perl 6 but what David Skoll wrote here:
[http://david.skoll.ca/blog/2010-07-29-perl-
sss.html](http://david.skoll.ca/blog/2010-07-29-perl-sss.html)

Quote:

"I asked on a forum what the goals are for relative size and speed of Perl 6
vs. Perl 5, and a Perl 6 developer responded that a reasonable goal would be
to have Perl 6 be twice as big as Perl 5 and take twice as long to start up.

"To achieve this goal, the Perl 6 developers will have to shrink the program
size by a factor of 6.1 (that is, get rid of about 84% of the code.) They'll
need to reduce startup memory consumption by a factor of 13.7 (that is, cut
out 93.7% of their memory use) and reduce startup time by a factor of over
275.

"Oh, and this is after they add in all the missing features required to bring
Perl 6 up to production-level."

Has the situation gotten better since 2010?

~~~
raiph
> Has the situation gotten better since 2010?

Not really. Startup uses about the same RAM. It's about 10x faster.

The best docs I know about performance would be
[http://pmichaud.com/2012/pres/yapcna-
perflt/slides/slide17.h...](http://pmichaud.com/2012/pres/yapcna-
perflt/slides/slide17.html) and [http://jnthn.net/papers/2014-yapceu-
performance.pdf#page=72](http://jnthn.net/papers/2014-yapceu-
performance.pdf#page=72)

> "... all the missing features required to bring Perl 6 up to production-
> level."

The latest story is that the last major missing features (Unicode grapheme-by-
default and native arrays) will land in the next few months and Perl 6 will be
declared "officially ready for production use" by the end of 2015.

------
JeremyMorgan
Sed is one of those tools that once you learn it, you'll start to wonder how
you ever got by without it.

This is a great set of tutorials, he also wrote one about Awk:
[http://www.grymoire.com/Unix/Awk.html](http://www.grymoire.com/Unix/Awk.html)

Get to know these two tools and you'll be amazed at the hours you can save and
what you can do, especially with text files.

~~~
sqrt17
Maybe it's because Perl was really popular at the time I discovered Unix and
its tool, but why would you use sed and awk instead of a Perl one-liner? (Or
even :s// in vim or M-x query-replace-regexp in emacs, if it's just regex
munging)

~~~
emmelaich
Exactly. Use Perl (or maybe Python).

sed is bullshit.

Do you know it is _not actually possible_ to get output from sed that does not
contain a newline?

~~~
Tobu
Are you sure? sed has -z, it's useful.

------
sidcool
I tried posting this to /r/programming. It said already submitted 8 years ago.
It's a very good tutorial and I was oblivious of its existence for so long.

------
c3RlcGhlbnI_
To be honest I don't know how much he has improved on the manual. It is such a
small language that you could easily read up to the the examples very quickly
even if you aren't particularly interested in learning.

I would suggest just giving it a look directly at
[https://www.gnu.org/software/sed/manual/sed.html](https://www.gnu.org/software/sed/manual/sed.html)

Though be forewarned, something that neither document explains well is the
actual syntax. As in how addresses and expressions can be used and how to read
a script. The syntax is relatively simple to understand looking at some
examples, but the lack of clear delimiters between the address, command, and
command parameters can confuse beginners.

~~~
josteink
But that only covers the GNU bits.

If you stick (too closely) to that one, sometimes you might use features only
found in GNU Sed and think you're writing portable scripts.

I think this tutorial helps clarify what is and isn't portable.

~~~
c3RlcGhlbnI_
Well, it does mention explicitly which parts are GNU extensions. But I see
what you mean. I can write sed scripts fairly well at this point but clearly
didn't internalize any of the notes about what is an extension, thus it is
frustrating trying to get the non GNU versions to do anything at all.

------
senorsmile
probably the best reference besides the o'reilly books.

------
sea6ear
Peter Krumins also has decent a walkthrough of sed that essentially goes
through and explains it via detailed explanations of sed one-lines (The
explanations are original, but the list of one-liners was already popular on
the internet).

Book: [http://www.catonmat.net/blog/sed-
book/](http://www.catonmat.net/blog/sed-book/)

Free online articles: [http://www.catonmat.net/blog/sed-one-liners-explained-
part-o...](http://www.catonmat.net/blog/sed-one-liners-explained-part-one/)

------
mbubb
For context of 'why sed and why not x?'

This was written in 1984 (I think) and still works with a few syntax
adjustments. I think it is not bad discipline to return to these tools from
time to time and remember core UNIX principles.

[http://web.stanford.edu/class/cs124/kwc-unix-for-
poets.pdf](http://web.stanford.edu/class/cs124/kwc-unix-for-poets.pdf)

I am not so sure anything that I currently am writing would/ could be relevant
in 30 years. Very humbling.

------
Jacky800
I have always used grymoire.com for regex and sed tutorials since probably
around 2009. Thanks grymoire

------
jakeogh
Is there a command to automatically escape a string for use in sed?

I got frusturated escaping for simple replacement:
[https://github.com/jakeogh/replace-text](https://github.com/jakeogh/replace-
text)

~~~
klshxsh
If you find yourself having to escape a lot of slashes, then you can do
something like this:

echo "/foo/bar" | sed -e 's|/foo|/tmp|'

(The article mentions this)

~~~
JetSpiegel
It can be any character really. I like sed 's,foo,bar,' sed 's@foo@bar@'

------
nkangoh
A friend of mine has been trying to get me to learn awk, sed, perl or grep.
Honestly I only have the patience for one at the moment, which do you think is
the best (taking the ease of learning into account)?

~~~
jibberia
Learn grep. If you're familiar with using a shell, it'll be the most
immediately useful.

# search current and all child directories for files containing "bananas",
case insensitive $ grep -ir "bananas" .

~~~
scarecrowbob
here's an example of the first useful grep I learned :

history | grep "git push"

~~~
jpwgarrison
I alias "history | grep" to "hrep" and I'd bet it is one of my top 3-5
commands.

~~~
spdustin
Why not find out:

    
    
      history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

~~~
jakupovic
uniq seems a bit easier to understand here:

history | awk '{print $2}' | sort | uniq -c | sort -rn

------
stygiansonic
Great reference; with the awk reference on that site, this formed a great
resource for learning text manipulation/searching in a Unix environment.

------
michaelsbradley
There's also an excellent (non-free) book:

 _Definitive Guide to sed_

[http://www.sed-book.com/](http://www.sed-book.com/)

I found it to be well worth the money, though I wish it were available as a
PDF.

------
la6470
Awesome

------
misiti3780
was interested in reading this until i saw the yellow background and couldnt
stomach it - looks like a lot of great information just displayed in a
horrible way

~~~
mdaniel
If it's available on your platform, you could print it to a local file since
the print version doesn't use the same CSS as the web (OS X = print to PDF,
Win = print to XPS, etc).

Or, presuming you're on a modern browser and care that much about the content,
you can just inspect the dom, find that <link type="text/css"...> in the head,
and delete it.

~~~
nilved
> If it's available on your platform, you could print it to a local file since
> the print version doesn't use the same CSS as the web (OS X = print to PDF,
> Win = print to XPS, etc).

Yeah or you could go read a better book

~~~
gh02t
You'll have a hard time finding one though. The O'Reilly books are the only
ones that I know of that might qualify as "better."

The site is ugly, but it's one of the best references for sed.

