Opinions may not reflect my employment or other affiliations

Translate

Saturday, 18 January 2014

9 Ways To Sort Files In Linux Environment



Sorting files is important for meaningful output and is useful while dealing with DB files, CSV, xls, log files and text files. By default, the sort command will sort according to alpha-bates, sorting according to single character vertically. If it finds the same character in two lines, then it will move on to sort the second character.

There are 9 ways to do that according to www.linuxnix.com. You can mix and match them as per your requirement.

• 1:To sorting file names according to alpha-bates: sort filename.txt.

• 2: If you have a file with host names in the third column, to sort it according to the column, use K for sorting that. Like, sort k3 filename.txt.

• 3: To sort/ etc/password file according to home directories, sort will by default take space/tabs as field separators. In etc/passwd file, the field separator is : and this can be done with t option. sort -t: -k6 /etc/passwd 

• 4: To sort according to number, etc/passwd file according to UID, use -n option to do that. Sort will not understand numbers by default, so, use -n to make sure sort command understands it.sort -n -t: -k3 /etc/passwd. Without -n option sort will by default sort only first numerical char. 



• 5: Sort the file and reverse the order. sort -r filename.txt

• 6: Sometimes, it is required to sort the file and display only unique values. sort -u filename. 

• 7: To sort a file according to your requirement and save it to a different file, use -o option, to save the sorted output to a file. sort -o temp.txt filename.txt

• 8: If you have file content with sizes like 10K, 20G, 45M, 32T etc, you can sort according to human readable by using -h option. This will work on RHEL5 and above versions. sort -h filename.txt 

• 9: To check if the file is already sorted or not by using -c option. This will show you the first occurrence value. sort -c filename.txt 

No comments :

Post a Comment