Results 1 to 7 of 7
Hi everyone
I have found myself stuck whilst messing about with files and what can be done with them.
I have a file which contains only 6 words in 1 ...
- 03-04-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 5
translate
Hi everyone

I have found myself stuck whilst messing about with files and what can be done with them.
I have a file which contains only 6 words in 1 line.
I want to sort each word of that line in alphabetical order
and display the result in a different file with each word taking up one line each.
I know that tr ' ' ' ' '\n will filter input and translate it (in this case any SPACE character in the input and translate that into a LINE-FEED). but I cannot get the format correct.
I tried cat six.txt | tr ' ' ' ' ' '\n' | sort > sortsix.txt but this does not work and I do not know why.
can anyone please help?
Thank you.
- 03-04-2008 #2
So the way that tr works is that it takes in two sets of characters and translates each character of the first into the corresponding character of the second. For instance:
turns A => a, B => b, C => c.Code:tr 'ABC' 'abc'
The way you have it, you're giving tr three sets: the first two consist of a single space, and the third consists of a space and a newline.
This works for me:
Note that I'm doing this on a Mac OS X machine (I can't seem to SSH into my Linux box at the moment), but it should work very similarly, if not the same.Code:tanya:~ alex$ echo "hello bye" | tr ' ' '\n' hello bye
DISTRO=Arch
Registered Linux User #388732
- 03-04-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 5
Thanks for your response.
I am at work just know but I will try as soon as I get home.
thanks again.
- 03-04-2008 #4Just Joined!
- Join Date
- Mar 2008
- Posts
- 5
srory jsut nitoecd my sepillnig msaitke.
- 03-04-2008 #5Just Joined!
- Join Date
- Mar 2008
- Posts
- 5
Thank you very much Cabhan.
I was struggling to make sense of that and your explanation really helped.
this worked
Thanks again.Code:tr [' '] ['\n'] < six.txt | sort > listsix.txt
- 03-05-2008 #6
Glad it's working for you!
Just want to say: you don't need the '[...]' around the sets. In documentation, when you see '[...]' in a command, it means that this part is optional.
In Bash, when you use '[...]', it means that this is a character class, basically a way of matching filenames. In your case, it makes no difference, since it's a single character, but if I said something like:
it would list all files that started with 'a', 'b', or 'c'.Code:ls -l [abc]*
So yeah. Just don't want you to be confused if you get strange results in the future.DISTRO=Arch
Registered Linux User #388732
- 03-05-2008 #7Just Joined!
- Join Date
- Mar 2008
- Posts
- 5
Thats great, tried it again without the [] everything was fine.
I am still really new to linux and I find it takes a bit of getting used to but I am getting there.
things are made a bit easier when there are places like this to come for help.


Reply With Quote