Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    tr 'ABC' 'abc'
    turns A => a, B => b, C => c.

    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:
    Code:
    tanya:~ alex$ echo "hello bye" | tr ' ' '\n'
    hello
    bye
    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.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just 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.

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    5
    srory jsut nitoecd my sepillnig msaitke.

  5. #5
    Just 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

    Code:
    tr [' '] ['\n'] < six.txt | sort > listsix.txt
    Thanks again.

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    ls -l [abc]*
    it would list all files that started with 'a', 'b', or 'c'.

    So yeah. Just don't want you to be confused if you get strange results in the future.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...