Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
TetraWiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linux 101 hacks
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=='''Hack 23. Sort Command'''== Sort command sorts the lines of a text file. Following are several practical examples on how to use the sort command based on the following sample text file that has employee information in the format: employee_name:employee_id:department_name. $ '''cat names.txt''' Emma Thomas:100:Marketing Alex Jason:200:Sales Madison Randy:300:Product Development Sanjay Gupta:400:Support Nisha Singh:500:Sales '''Sort a text file in ascending order''' $ '''sort names.txt''' Alex Jason:200:Sales Emma Thomas:100:Marketing Madison Randy:300:Product Development Nisha Singh:500:Sales Sanjay Gupta:400:Support '''Sort a text file in descending order''' $ '''sort -r names.txt''' Sanjay Gupta:400:Support Nisha Singh:500:Sales Madison Randy:300:Product Development Emma Thomas:100:Marketing Alex Jason:200:Sales '''Sort a colon delimited text file on 2<sup>nd</sup> field (employee_id)''' $ '''sort -t: -k 2 names.txt''' Emma Thomas:100:Marketing Alex Jason:200:Sales Madison Randy:300:Product Development Sanjay Gupta:400:Support Nisha Singh:500:Sales '''Sort a tab delimited text file on 3rd field (department_name) and suppress duplicates''' $ '''sort -t: -u -k 3 names.txt''' Emma Thomas:100:Marketing Madison Randy:300:Product Development Alex Jason:200:Sales Sanjay Gupta:400:Support '''Sort the passwd file by the 3<sup>rd</sup> field (numeric userid)''' $ '''sort -t: -k 3n /etc/passwd | more''' root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin '''Sort /etc/hosts file by ip-addres''' $ '''sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts''' 127.0.0.1 localhost.localdomain localhost 192.168.100.101 dev-db.thegeekstuff.com dev-db 192.168.100.102 prod-db.thegeekstuff.com prod-db 192.168.101.20 dev-web.thegeekstuff.com dev-web 192.168.101.21 prod-web.thegeekstuff.com prod-web '''Combine sort with other commands''' o '''ps βef | sort''' : Sort the output of process list # '''ls -al | sort +4n ''': List the files in the ascending order of the file-size. i.e sorted by 5<sup>th</sup> filed and displaying smallest files first. # '''ls -al | sort +4nr ''': List the files in the descending order of the''' '''file-size. i.e sorted by 5<sup>th</sup> filed and displaying largest files first.
Summary:
Please note that all contributions to TetraWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
TetraWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)