It is not always essential to run sort command on a file. Sort the contents of file ‘ lsl.txt‘ on the basis of 9th column (which is the name of the files and folders and is non-numeric). Option ‘ -n‘ must be used when we wanted to sort a file on the basis of a column which contains numerical values.Ĩ. Note: The ‘ -n‘ option in the above example sort the contents numerically. Sort the contents of file ‘ lsl.txt‘ on the basis of 2nd column (which represents number of symbolic links). Now will see examples to sort the contents on the basis of other field and not the default initial characters.ħ. $ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt We are going a create a new file ( lsl.txt) at the same location for detailed examples and populate it using the output of ‘ ls -l‘ for your home directory. $ sort -r tecmint.txt > reversesorted.txtĦ. Also check the content listing of the newly created file. Now sort the contents of text file ‘ tecmint.txt‘ in reverse order by using ‘ -r‘ switch and redirect output to a file ‘ reversesorted.txt‘. Sort the contents of the file ‘ tecmint.txt‘ and write it to a file called ( sorted.txt) and verify the content by using cat command. Note: The above command don’t actually sort the contents of text file but only show the sorted output on terminal.Ĥ. Now sort the content of the file using following command. Before we start with ‘ sort‘ lets have a look at the contents of the file and the way it look. $ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txtĢ. The option ‘ -e‘ in the below command enables interpretion of backslash and /n tells echo to write each string to a new line. Our working directory is ‘ /home/$USER/Desktop/tecmint. First we will be creating a text file ( tecmint.txt) to execute ‘ sort‘ command examples. This article aims at deep insight of Linux ‘ sort‘ command with 14 useful practical examples that will show you how to use sort command in Linux.ġ. It is important to notice that sort command don’t actually sort the files but only print the sorted output, until your redirect the output. Sort command takes blank space as field separator and entire Input file as sort key. Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |