LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2009, 04:16 PM   #1
crispyleif
Member
 
Registered: Mar 2005
Location: Norway, by the coast
Distribution: Debian and the likes
Posts: 190

Rep: Reputation: 31
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 ?
 
Old 02-09-2009, 04:30 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
You could do it with 'tr'

Quote:
cat file | tr '[[:lower:]]' '[[:upper:]]'
 
Old 02-09-2009, 04:37 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
A sed command would be:
Quote:
sed -e 's/\(.*\)/\U\1/' file
U - lower to upper
L - for upper to lower cases
 
Old 02-09-2009, 04:47 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
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
 
Old 02-09-2009, 04:57 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by crispyleif View Post
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
 
Old 02-09-2009, 05:48 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Also, hheck out this sed tutorial. Someone recommended it on this forum before. It's got a lot of useful information.
 
Old 02-10-2009, 01:59 AM   #7
crispyleif
Member
 
Registered: Mar 2005
Location: Norway, by the coast
Distribution: Debian and the likes
Posts: 190

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Tinkster View Post
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
 
Old 02-10-2009, 02:00 AM   #8
crispyleif
Member
 
Registered: Mar 2005
Location: Norway, by the coast
Distribution: Debian and the likes
Posts: 190

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by sycamorex View Post
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
 
Old 02-10-2009, 03:20 AM   #9
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 63
Quote:
Originally Posted by sycamorex View Post
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.
 
Old 02-10-2009, 04:08 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Quote:
Originally Posted by theYinYeti View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
removing new line character using sed Fond_of_Opensource Linux - Newbie 4 03-26-2010 03:19 PM
awk does not seem to recognize character classes new_2_unix Linux - Newbie 6 10-15-2007 06:36 AM
HP-UX newline character with sed jhwilliams Other *NIX 8 08-06-2007 06:13 PM
sed insert newline character jhwilliams Linux - Software 6 06-08-2007 04:13 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration