LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed and character classes , strange results (https://www.linuxquestions.org/questions/linux-newbie-8/sed-and-character-classes-strange-results-703429/)

crispyleif 02-09-2009 03:16 PM

sed and character classes , strange results
 
file "file" contains a aa aaa and sed -e 's/[[:lower]]/b/g' file gives :

b bb bbb

all good.

sed -e 's/[[:lower:]]/[[:upper:]]/g' file gives :

[[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]]

Character classes seems not recognized. I have tried with LC_LANG=C / us / no.

Any ideas ?

sycamorex 02-09-2009 03:30 PM

You could do it with 'tr'

Quote:

cat file | tr '[[:lower:]]' '[[:upper:]]'

sycamorex 02-09-2009 03:37 PM

A sed command would be:
Quote:

sed -e 's/\(.*\)/\U\1/' file
U - lower to upper
L - for upper to lower cases

sycamorex 02-09-2009 03:47 PM

Sed has also got a 'y' command that could do it, but the solution is quite weird:

Quote:

sed 'y/abcdefghijklmnoprstuwxyz/ABCDEFGHIJKLMNOPRSTUWXYZ/' file
It doesn't seem to accept ranges:(

Tinkster 02-09-2009 03:57 PM

Quote:

Originally Posted by crispyleif (Post 3437568)
file "file" contains a aa aaa and sed -e 's/[[:lower]]/b/g' file gives :

b bb bbb

all good.

sed -e 's/[[:lower:]]/[[:upper:]]/g' file gives :

[[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]] [[:upper:]]

Character classes seems not recognized. I have tried with LC_LANG=C / us / no.

Any ideas ?

Character classes are valid in regular expressions,
which the replacement string isn't. See sycamorex
post for "how to" ;}


Cheers,
Tink

sycamorex 02-09-2009 04:48 PM

Also, hheck out this sed tutorial. Someone recommended it on this forum before. It's got a lot of useful information.

crispyleif 02-10-2009 12:59 AM

Quote:

Originally Posted by Tinkster (Post 3437616)
Character classes are valid in regular expressions,
which the replacement string isn't. See sycamorex
post for "how to" ;}


Cheers,
Tink

THIS solved it.. I've used it in ranges/regexp and didn't think about it being nor valid in a replacement string.

Big thanks to all, checking out the howto:)

crispyleif 02-10-2009 01:00 AM

Quote:

Originally Posted by sycamorex (Post 3437591)
A sed command would be:


U - lower to upper
L - for upper to lower cases

This one was new to me, looking forward to test it

theYinYeti 02-10-2009 02:20 AM

Quote:

Originally Posted by sycamorex (Post 3437591)
A sed command would be:
sed 's/…/\U\0/'
U - lower to upper
L - for upper to lower cases

It works! How comes it is not in the sed man page? How are we supposed to guess such things? Thank you sycamorex, this will save me from tedious and ineffective y//…

Yves.

jschiwal 02-10-2009 03:08 AM

Quote:

Originally Posted by theYinYeti (Post 3438049)
It works! How comes it is not in the sed man page? How are we supposed to guess such things? Thank you sycamorex, this will save me from tedious and ineffective y//…

Yves.

It is in the info manual. Keep in mind that it is a GNU extension so it isn't portable.


All times are GMT -5. The time now is 03:56 PM.