InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Linux/Unix Command Line tutorials

  • Awk
  • Curl
  • Edit a file without changing its timestamp on Linux
  • Find which process is listening on a port on Linux
  • Get file size in bytes on Linux
  • How to delete files starting with dash/hyphen
  • How to kill unresponsive ssh session using escape sequence
  • How to show environment variable for a process id (pid)
  • How to sort using a specific field on Linux
  • How to specify environment variable for a command on Linux
  • How to zip/unzip a directory with password
  • ImageMagick
  • Impact on LC_ALL on Linux sort
  • Linux - find listening ports
  • Linux - find top directories by used disk size (excluding size of subdirectories)
  • Linux - how to run a command as different user
  • Linux - list only directories
  • Linux - providing sudo access to a users - some best practices
  • Linux - sending mail from command using mailutils
  • Linux file timestamps
  • Linux how to modify a user using usermod
  • Linux ping a port using netcat
  • Linux replace comma with newline
  • Linux screen - quick start guide
  • Linux what package provides a file
  • Linux/Unix - How to go to previous directory
  • Linux/Unix - find inode number of a file
  • Linux/Unix - truncate a large log file without deleting it
  • Linux/Unix history with date and time
  • Memcache - how to dump all keys and values on command line
  • Mongo - cli quick start guide
  • Perl command line - replace multi line comments
  • Python/Perl/Unix one liners
  • Rsync
  • Ruby gem - handy reference
  • SSH
  • Some handy linux gnu date commands
  • Use watch to monitor a command at some frequency on Linux
  • bower - installation and quick start guide
  • csvkit - parse csv file and data on Linux command line
  • ffmpeg
  • grep without regex (fixed string)
  • redis cli quick start tutorial
  • wget handy commands

Linux find

  • Find recently modified files on Linux
  • Linux - find and delete files older than 30 days
  • Linux - find files containing specific text
  • Linux find - ignore case in name
  • find - exclude directory or file pattern
 
  • Home
  • > Tutorials
  • > Linux/Unix Command Line

How to sort using a specific field on Linux

By admin on Oct 25, 2015

Sometime we need to sort a file on a specific field. This may be useful if a file is in csv or tsv formart (probably an export from excel) and we need to sort it on a specific fields or columns. Here are some handy sort command on Linux for sorting on specific fields.

First we’ll set environment variable LC_ALL=C to ensure we use simple byte comparison for sorting:

$ LC_ALL=C

Basic sort

To do the basic sort run the following command:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort
aa bb 11 cc
mm nn 2 pp

Sort starting from a specific field

To sort the input starting from third field onwards and use space as delimiter, run the following command:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3
aa bb 11
cc dd 2

To sort only using 3rd field, you can use the following command:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3,3
aa bb 11
cc dd 2

Numeric sort starting from a specific field

To do numeric sort from a specific field, add -n as shown below:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -n -k3
mm nn 2 pp
aa bb 11 cc

You can also use the following alternate format:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3n
mm nn 2 pp
aa bb 11 cc

Reverse numeric sort on a specific field

To do reverse numeric sort from a specific field, add -n and -r as shown below:

$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3,3nr
mm nn 2 pp
aa bb 11 cc

Sort using multiple fields

You can also sort using multiple fields and also select numeric or reverse for each field as shown below:

$ printf "aa bb 11 cc\nmm nn 2 pp\nqq nn 2 pp\n" | sort -t " " -k3,3n -k1,1r
qq nn 2 pp
mm nn 2 pp
aa bb 11 cc

Note that in case we specify multiple field using -k, the order is also relevant and will have an impact on the outcome.

Suggested posts:

  1. DIG quick start tutorial for DNS Lookup
  2. Linux replace comma with newline
  3. How to setup MailChimp Rss email campaign
  4. Vim – how to go back to last edited line/context
  5. Perl command line – replace multi line comments
  6. Python/Perl/Unix one liners
  7. HTML li tag
  8. How to show environment variable for a process id (pid)
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Linux/Unix Command Line, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress