- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
How to sort lines of text files in Linux?
To sort lines of text files, we use the sort command in the Linux system.
The sort command is used to prints the lines of its input or concatenation of all files listed in its argument list in sorted order. The operation of sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as the sort key.
The general syntax of the sort command is as follows.
sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F
Brief description of options available in the sort command.
|Sr.No.||Option & Description|
Ignore leading blanks.
Consider only blanks and alphanumeric characters.
Fold lower case to upper case characters.
|4||-g, --general-numeric-sort |
Compare according to general numerical value.
Consider only printable characters.
Compare (unknown) <’JAN’ <...<’DEC’.
Compare human-readable numbers.
Compare according to string numerical value.
Get random bytes from the FILE.
Reverse the result of comparisons.
Sort according to the WORD.
Display this help and exit
Output version information and exit.
Here, we will create a file using the cat command and sort this file using the sort command in the Linux system.
$ cat >text.txt Sid Vikash Gaurav ^C $ sort text.txt Gaurav Sid Vikash
Here, we will sort a file in the reverse order using the -r or --reverse option with the sort command in the Linux operating system.
$ cat >text.txt Sid Vikash Gaurav ^C $ sort text.txt Vikash Sid Gaurav
In the above example, we already saw that how can we sort a file but output of the sort command on standard output. Here, we will save output into a new file in the file system.
$ sort text.txt > newtext.txt
After executing the above command, a new file will be created with the newtext.txt name.
To check more information and options with descriptions about the sort command, we use the --help option with the sort command as shown below.
$ sort --help
To check in which version the sort command is working, we use the --version option with the sort command in the Linux system as shown below.
$ sort --version
- Related Articles
- How to merge lines of files in the Linux system?
- How to join lines of two files on a common field in Linux?
- Recursive Search and Replace in Text Files in Linux
- How to find and sort files based on modification date and time in linux
- How to remove sections from each line of files in the Linux system?\n
- Count Duplicate Lines in a Text File on Linux
- How to Count Number of Files in Linux
- Remove the Last N Lines of a File in Linux
- Counting number of lines in text file using java\n
- Set the limit of text length to N lines using CSS
- Linking to Files in Linux
- How to remove files and directories in the Linux operating system using the terminal?\n
- How to display multiple lines of text in Tkinter Label?
- How to retrieve a specific number of lines from files in PowerShell?
- How to Reverse Order of Lines in a File in Linux