Index ¦ Archives ¦ RSS

cheap sed & bash tricks

Estimated read time: 1 minutes

probably this is known, but i've just figured out how to get rid of grep when i use

foo |grep ... |sed ...
the magic command is
foo |sed -n '/^match/s/foo/bar/p'

if we're at it, you can even get rid of sed if you use bash and just want to do simple replaces like 1.0 -> 1_0:

$ pkgver=1.0
$ echo ${pkgver//./_}
1_0_0

two intereting tricks remained. the first is about deleting from a pattern to an another one (including both line)

$ echo -e 'a\nb\nc\nd\ne'
a
b
c
d
e
$ echo -e 'a\nb\nc\nd\ne' |sed /b/,/d/d
a
e

the last one is about deleting to the end of the file from a pattern:

$ echo -e 'a\nb\nc\nd\ne' |sed '/c/,$d'
a
b

ok, that's all for today :)

(other nice tricks here)

© Miklos Vajna. Built using Pelican. Theme by Giulio Fidente on github.