think tank forum

technology » sed/reg exp. help

sriehl's avatar
17 years ago
r1, link
sriehl
surreal
this post has been archived.
sriehl's avatar
17 years ago
r1, link
sriehl
surreal
this post has been archived.
sriehl's avatar
17 years ago
link
sriehl
surreal
Ok, I have a line that looks like this:
29760 pts/1 S+ 0:00 sh -c madplay -b 16 -R 44100 -S -o raw:- "/home/ices/music/Modest Mouse - Ocean Breathes Salty.mp3" | lame --preset cbr 96 -r -x -s 44.1 --bitwidth 16 - -

and I want to remove everything before /music/ and everything after .mp3"

I've been hacking away with sed and haven't gotten it to work yet. Maybe someone wants to take a crack at it?
sriehl's avatar
17 years ago
link
sriehl
surreal
damn it, I figured it out

sed -e 's/[: -+0-9a-zA-Z\"]*music\///g' -e 's/.mp3[: -+0-9a-zA-Z\"\|\-]*//'
asemisldkfj's avatar
17 years ago
link
asemisldkfj
the law is no protection
I probably hate regular expressions more than any other computer-related thing. I have NEVER been able to wrap my head around them and it frustrates me to no end when I try.

I need someone to sit me down and give me an hour-long crash course in them or something.
dannyp's avatar
17 years ago
link
dannyp
dʎuuɐp
ya when you do, try recoding the damn url regexp for ttf
sriehl's avatar
17 years ago
link
sriehl
surreal
ok, now that shit is starting to work and I *think* I understand it, I think its pretty cool.
 
17 years ago
link
dbrown
oh man, regex is where i thrive. probably my fav. thing in programming.
it's a sick addiction to line noise i guess
lucas's avatar
17 years ago
link
lucas
i ❤ demo
what happened to db-is-on-ttf-regexp?
 
17 years ago
link
dbrown
i started teaching a class...
dannyp's avatar
17 years ago
link
dannyp
dʎuuɐp
ttf url regexp assignment for students?
lucas's avatar
17 years ago
link
lucas
i ❤ demo
:D
sriehl's avatar
17 years ago
link
sriehl
surreal
dooo eeeet!
lucas's avatar
13 years ago
r1, link
lucas
i ❤ demo
how come the first one doesn't work but the second one does? i shouldn't be escaping a metacharacter. does the shell need it escaped, which then passes it to grep unescaped?

$ df | grep '^.* +/usr/home$'
$ df | grep '^.* \+/usr/home$'
/dev/concat/gc9a        1210925126 1031973254  82077862    93%    /usr/home
$
lucas's avatar
13 years ago
link
lucas
i ❤ demo
is there a way to use awk instead of both grep and awk for the following?

$ df | grep '^.* \+/usr/home$' | awk '{ sub(/%/,"",$5); print $5}'
93
$
lucas's avatar
13 years ago
r1, link
lucas
i ❤ demo
this is probably the best one to avoid false positives:

$ df | grep '^.*% \+/usr/home$' | awk '{ sub(/%/,"",$5); print $5}'
93
$


for example:

$ df
Filesystem              1K-blocks        Used     Avail Capacity  Mounted on
/dev/mirror/gm9s1a          507630     339824    127196    73%    /
devfs                            1          1         0   100%    /dev
/dev/mirror/gm9s1d          507630         22    466998     0%    /tmp
/dev/mirror/gm9s1e         2026030     379118   1484830    20%    /var
/dev/mirror/gm9s1f       231437172    2351436 210570764     1%    /usr
/dev/concat/gc9a        1210925126 1031973170  82077946    93%    /usr/home
//LUCAS@PENELOPIA/ATLAS  312560608   19527916 293032692     6%    /joke/ha   ha   ha   /usr/home
$ df | grep '^.* \+/usr/home$' | awk '{ sub(/%/,"",$5); print $5}'
93
6
$ df | grep '^.*% \+/usr/home$' | awk '{ sub(/%/,"",$5); print $5}'
93
$
lucas's avatar
13 years ago
link
lucas
i ❤ demo
ugh, even percent signs can be part of a directory name. :[
dannyp's avatar
13 years ago
link
dannyp
dʎuuɐp
i'm following along on atlas, trying some stuff out.
dannyp's avatar
13 years ago
r2, link
dannyp
dʎuuɐp


$ df | awk '/home/ {sub(/%/,"",$5); print $5}'
$ df | awk '/\/usr\/home/ {sub(/%/,"",$5); print $5}'

andre's avatar
13 years ago
link
andre
For the record, this works:

$ df | grep -E '^.* +/usr/home$'


From the grep manual: "In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \)."

"Basic regular expressions" here means what simply running "grep" uses. With -E it uses the "extended regular expressions". If you're lucky your grep supports -P and then you can use all the Perl stuff in the regexes.
lucas's avatar
13 years ago
link
lucas
i ❤ demo
oh. thanks!