Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I sort rows of text alphabetically without using specialized third party software or plugins (like Notepad++/Word/Excel/et al)?

I suppose what I'm perhaps asking is -- How do I sort rows of text alphabetically through a CLI (either Linux or Windows)?

I have a long list of text that I'd like to sort alphabetically (dumping a router's nvram variables with nvram export --set), for example:

nvram set wan_unit="0" 
nvram set NC_Verbosity="2" 
nvram set sesx_led="0" 
nvram set led_override="" 

It doesn't matter how it's accomplished, I just need to visually see the rows in alphabetical order. I have access to both Linux and Windows (the text originates from a Linux system, which I've copied/pasted into Windows Notepad, but I can do the sorting in either one).

share|improve this question

You can do this in PowerShell by running the following command:

Get-Content .\foobar.txt | Sort | Out-File sorted.txt

Leave off the Out-File command to see it in the console.

share|improve this answer

On most linux systems its going to be something like

cat fileToSort.txt | sort > sorted.txt

Simply type that into the terminal in the directory with the file you want to sort, 'fileToSort.txt', and the sorted result is put into 'sorted.txt' in the same directory.

Here's what all the parts mean: 'cat' is a command which takes a file and outputs its contents to a terminal. '|' takes the terminal output of the program to the left, and rerouts it into the input of the program on the right. 'sort' sorts its input alphanumerically and prints the result into the terminal. '>' takes the output of the program to the left, and places it into the file on the right. The above thus reads "output the contents of fileToSort into the input of the 'sort' program, and reroute the output of that into sorted.txt".

The Linux environment is very good at doing this kind of thing - you can do A LOT of officy-type tasks by gluing together existing Linux programs. For more neat stuff, I recommend reading "The Unix Programming Environment" by Pike. Its a bit dated, but 90% of the content is absolutely fine, and its really well written.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .