Earlier this month I made a New Year's Resolution, that I'm going to learn to use unix a little bit better. Well, so far I've made some good progress as in the past week I have been starting to get used to the sed command.
I find it quite handy; how did I ever get along without it? The one thing that's annoying is that it isn't pcre-compliant. That would make it a lot easier for me to use. I end up having to string multiple sed commands together in order to get it to work properly.
I understand there is a pcrsed command, but it's not installed on the machine I use at work where it would be useful to me.
If you know Perl, try using "perl -pe" instead.
It's *much* more flexible and s/foo/bar/ which is
all most people do with sed works fine. Most other
basic sed operations can be done one way or another
and it's much more powerful (perl regular
expressions are just one example).
perl -pe 's/foo/bar/' is the same as sed 's/foo/bar/'
"perl -lne" or "perl -ne" is another great command.
For example:
perl -lne 'print $1 if /foo=([\w.-]+)/' will print
some stuff coming after "foo" on the line. This
could also be done with s// but you can do stuff
like this which is handy:
perl -lne '$x += $1 if /(\d+)/; END { print $x; }'
Some people use awk for stuff like that, but it's
easier to just use perl, I think.
Thanks a lot for the tip!