Log in

No account? Create an account
My life would be perfect right now if I didn't have to try to learn… - Chronicles of a Hereditary Geek [entries|archive|friends|userinfo]
Darth Paradox

[ website | Pyrlogos - a fantasy webcomic ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Oct. 19th, 2003|12:08 pm]
Darth Paradox
[mood |ecstaticecstatic]
[music |REM - Hope]

My life would be perfect right now if I didn't have to try to learn regexp. I mean, I understand regular expressions... but actually using them in UNIX? so confusing.

But... wow. life is good.

From: dacut
2003-10-19 12:38 pm (UTC)
Heh. Aren't they awfully inconsistent? Especially when you try to remember which tool wants \( \) versus ( ) for grouping. And then, if you have to worry about shell escapes, they become \\\( \\\)... ugh.
(Reply) (Thread)
[User Picture]From: darthparadox
2003-10-19 03:22 pm (UTC)
Yes. and I'm working in csh, and I tried both ( ) and \( \) and neither seemed to work... sigh.

(Reply) (Parent) (Thread)
From: dacut
2003-10-19 05:35 pm (UTC)
If you don't need to use environment variables ($HOME or whatever), use single-quotes. Anything between them will not have shell substitutions applied to them.

For example (I'm using tcsh), the following:

echo "bbbaaabb" | sed -e 's/b*\(a*\)b*/\1/'

produces "aaa"

sed expects grouping operators as \( \) . ( and ) are literals.
(Reply) (Parent) (Thread)
[User Picture]From: darthparadox
2003-10-19 10:27 pm (UTC)
What does \1 do?
(Reply) (Parent) (Thread)
From: dacut
2003-10-19 10:33 pm (UTC)
\1 is replaced by the contents inside the first \( \) grouping (similarly for \2, \3, etc.). Hence why it printed out the "aaa".

s is the substitute command in sed; format is "s/search/replace/" or "s/search/replace/g" (where g means "global," i.e., replace all matches found, not just the first one).

You can use a character other than / to separate the parts. For example, using a comma makes it easier to type pathnames, e.g.:

sed -e 's,/home/dacut/,/home/darthparadox/,g'

will replace all occurrences of my home directory with yours.
(Reply) (Parent) (Thread)
[User Picture]From: mcmartin
2003-10-19 04:21 pm (UTC)
Well, you can drop the number of escapes if you put things in quotes, at least in bash...
(Reply) (Parent) (Thread)
[User Picture]From: inprotest
2003-10-20 05:34 am (UTC)
sounds fun
what's all this craziness for?
(Reply) (Thread)