With the terminal: Alphabetize the content (lines) of a file

In my spare time I start to randomly check system commands ... that's why I often find interesting tips 🙂

This is one of them, precisely as the title indicates, I will show you how having a file with many lines of text inside, alphabetize the lines of that file.

For example, we have a file (called distros) containing the following:

linux
debian
ubuntu
archlinux
sabayon
gentoo
soluses

And we want to alphabetize these distros.

To do this in a terminal we put the following:

cat distros | sort > distros-ordenadas

And voila, I show you the screenshot of me executing this command, and then doing a cat (showing content) of the new file ordered-distros:

…. To what is extremely easy? 😀

The command Black you have many options, in the terminal if they do mansort it will show you all the options 😉

Anyway, a little tip that could solve several problems at a certain time hahahaha, I hope you liked it.

regards


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Cris Nepita said

    This is very useful, thanks for sharing the information ~

    1.    KZKG ^ Gaara said

      Thanks to you for commenting 😉

  2.   Augustine said

    Good tip 😉

    1.    KZKG ^ Gaara said

      Thank you

  3.   pipe said

    Where do you get the tips from?

    1.    KZKG ^ Gaara said

      Just as I said at the beginning of the post, in my spare time I start to review system commands 🙂
      I found this one because I was looking for options or alternatives to the command locate, I tried searching for commands like "search" or something like that, but didn't find any, so I listed all the commands with s and after a while it caught my attention Black : )

      Thanks for the comment, welcome to the blog 😀

      1.    Will said

        Alternatives to "locate", you have the "whereis" and "find"

        1.    KZKG ^ Gaara said

          Yep, find I knew him ... but the whereis nop, hehe thanks 😀

  4.   Will said

    I support Unix AIX and Sun servers (and from time to time a Red Hat or SuSe) entirely from the terminal and the truth is that the sort commands, along with cut, grep, awk, etc. they do wonders ^ ___ ^

  5.   Darry castro said

    Excellent, I have used it for a format of this model

    100: user1
    287: user2
    150: user3

    cat order.txt | sort -n> order1.txt
    100: user1
    150: user3
    287: user2

    Thank you…

  6.   Ernesto said

    Thanks, I was able to apply it correctly.

  7.   Eugenia said

    How do I get it to write it to the same file, that is, without having to assign it to a new file? Thank you!

    1.    teacher said

      If I don't misunderstand, for it to be modified in the same file is like redirecting the output to the file with the same name. BUT BEWARE! If you sent yourself a baton and you want to go back, you can't (the content is rewritten).

      In the example they left us it would be:
      cat distros | sort> distros

  8.   teacher said

    I don't know if the author of this post will see the comment, but I had a question with the «sort» command ...
    In a file I put the list of directories that contained a certain file. Those directories have numbers in their name, so I use numbers like 100, 10, 1, .1 and .01
    When you order them it makes everything ALMOST perfect:
    1) It does not order from /.01, /.1, / 1, / 10 and / 100 (sorts like this:… / .01,… / 100,… / 10, but with internal alphabetical coherence of their respective subfolders)
    2) Toggle one and one between… / .1 and… / 1

    Can you think of how I can make the order look perfect or is it a limitation of the sort command?

    Thank you in advance