ultra simple question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ultra simple question
# 1  
Old 11-12-2006
ultra simple question

hey,

i want to use tr to remove all the lowercase characters from a string and replace them with a '.', i can't seem to get it to work, heres an example of what im trying, someone please correct me

: tr [a-z] .
# 2  
Old 11-12-2006
Use sed instead...

Code:
$ echo 123abcABC456aaaaAAAAAZxCvBnM | sed 's/[a-z]/./g'
123...ABC456....AAAAAZ.C.B.M
$

# 3  
Old 11-12-2006
cheers

thanks for that mate, that was giving me retard attacks Smilie
# 4  
Old 11-12-2006
I am not sure why you were having problem with your tr statement.

It looks perfectly ok to me, have a look

----------------------------------------------------
$ echo AmiT|tr [a-z] .
A..T
----------------------------------------------------

and this is what you were doing. Right ??

Thanks,
Amit
kamitsin
# 5  
Old 11-12-2006
If the current directory contains file(s) with a name formed by a signle lowercase character the shell will substitute [a-z] by the file list.
Code:
tr '[a-z]' .

Jean-Pierre.
# 6  
Old 11-12-2006
Quote:
Originally Posted by aigles
If the current directory contains file(s) with a name formed by a signle lowercase character the shell will substitute [a-z] by the file list.
Code:
tr '[a-z]' .

Jean-Pierre.
Yeah, but even with those quotes, there are still problems. tr varies quite a bit from system to system. And, as per tradition, the orignal poster provided no clues about his version of unix. According to the Posix Standard
Quote:
c-c
In the POSIX locale, this construct shall represent the range of collating elements between the range endpoints (as long as neither endpoint is an octal sequence of the form \octal), inclusive, as defined by the collation sequence. The characters or collating elements in the range shall be placed in the array in ascending collation sequence. If the second endpoint precedes the starting endpoint in the collation sequence, it is unspecified whether the range of collating elements is empty, or this construct is treated as invalid. In locales other than the POSIX locale, this construct has unspecified behavior.
So if the tr program is following the standard, you will convert [ and ] into periods for sure. And if the locale is set to Posix, the lower case letters will also be converted into periods as well. There are still some non-standard version of tr that require '[a-z]' for a range and treat 'a-z' as 3 characters. Among the tr programs that accept a-z, there are two behaviors for '[a-z]':
[a-z] == a-z
[a-z] == ]a-z[
with the second being what the standard requires. With a non-standard locale, the author of the locale determines what a-z means. The confusion over a-z verses [a-z] arises from the fact that BSD used a-z and System V used [a-z]. Posix had to pick one and the went with BSD. Apparently not everyone was happy with choice and compliance with this requirement is not universal.

Now that we have discussed the difficulty with the first parameter, let's look at the second parameter. We have a second System V/BSD clash and this time Posix wimped out. Whatever the first parameter means it is more than one character but the second parameter is guaranteed to be only one character. So the strings are not equal in length. According to the standard:
Quote:
When string2 is shorter than string1, a difference results between historical System V and BSD systems. A BSD system pads string2 with the last character found in string2. Thus, it is possible to do the following:
tr 0123456789 d
which would translate all digits to the letter 'd'. Since this area is specifically unspecified in this volume of IEEE Std 1003.1-2001, both the BSD and System V behaviors are allowed, but a conforming application cannot rely on the BSD behavior. It would have to code the example in the following way:

tr 0123456789 '[d*]'
So, to do exactly what the author intended,
tr '[a-z]' .
requires both the System V behavior or ignoring (or requiring) brackets around a range (a Posix violation) and BSD's "extend the last character of a shorter string" behavior (not guaranteed by Posix). A more correct solution is:
tr '[:lower:]' '[.*]'
but now you probably see why I prefer sed for something like this...

Good thing this question was "ultra simple"! Smilie
# 7  
Old 11-13-2006
hey, well i didn't expect my post to ellicit this much discussion i was just tearing my hair out because i thought that my command:

Quote:
echo $var | tr [a-z] .
should have been converting all of the lowercase chars to . What was happening which was confusing me so much, was that it was only converting the first lowercase char in the string, so it seemed like i was missing some obvious command, i tried all combinations of quotes etc and even misinformedly tried putting brackets around the . -> [.] :P the version of unix i was working on was solaris and the bourne shell, don't know if that helps explain anything, but im grateful for the help and have the solution already in place Smilie thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. UNIX for Dummies Questions & Answers

Simple question

I had a script in solaris wich i read data, for example: Number 1: _ and the cursor use to be in '_' place because in the code of the script i write: echo "Number 1:\c" but i copy the script to a linux and the cursor 'jump' to the begining of the next line like: Number 1:... (2 Replies)
Discussion started by: lestat_ecuador
2 Replies

3. Shell Programming and Scripting

Simple ls question

i am doing ls -la in the out put , first line is as total 41621 What is this total? (2 Replies)
Discussion started by: Saurabh78
2 Replies

4. UNIX for Dummies Questions & Answers

Simple Question

Hi Guys, I've been learning UNIX for the past couple of days and I came across this exercise, I can't get my head around it, so I would be ever so grateful if I could receive some sort of help or direction with this. Create a file with x amount of lines in it, the content of your choice. ... (3 Replies)
Discussion started by: aforball
3 Replies

5. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

6. UNIX for Dummies Questions & Answers

Very simple question

I'm new to unix commands and am wondering how you could create a page with html tags in it. echo "<b>Test</b>" > test.html doesn't work because of the tags. How would I do this. (4 Replies)
Discussion started by: roger19
4 Replies

7. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies

8. UNIX for Dummies Questions & Answers

Simple Question

Can anyone tell me if there is a way to remove the encryption from Data CDs by UNIX? Or does anyone know of a program that can remove the encryption? I would much appreciate it! Thanks, -Peaves (2 Replies)
Discussion started by: Peaves
2 Replies

9. UNIX for Advanced & Expert Users

a simple question

I have a problem, why should root access only be provided from a login on the console, and why should a user be forced to use "su" command to perform system maintenance, rather than logging in? (2 Replies)
Discussion started by: pnxi
2 Replies

10. UNIX for Advanced & Expert Users

Simple Question

Friends, I did following exercise $ echo '' > test $ od -b test $ echo "">test $ od -b test $echo > test $od -b test Every time I got the following output 0000000 012 0000001 But 012 is octal value for new line character . Even though there is no apperent new line character... (6 Replies)
Discussion started by: j1yant
6 Replies
Login or Register to Ask a Question