Get the App
SLTechnology News&Howtos  ›  Servers  › 

Regular expressions-sorting three tools (sort, uniq, wc)

Shulou Source: shulou.com Published: 2022-06-02 21:54:16 09月15日 Update

1. Sort tool

(1) sort is a tool for sorting file contents by behavior unit, and it can also be sorted according to different data types.

(2) sort command format: sort [option] parameter

(3) commonly used options:

-f: ignore case;-b: ignore spaces in front of each line;-M: sort by month;-n: sort by number;-r: sort in reverse;-u: equivalent to uniq, indicating that only one row of the same data is displayed;-t: specify a delimiter, separated by [Tab] key by default;-o: save the sorted results to the specified file

-k: specify the sort area

Example 1: sort the accounts in the / etc/passwd file

Sort / etc/passwd

Example 2: reverse sort the third column in the / etc/passwd file

Sort-t:-rk 3 / etc/passwd

Example 3: sort the third column in the etc/passwd file and save the output to the yyy.txt file

Sort-t:-k 3 / etc/passwd-o yyy.txt II, uniq tool

(1) the uniq tool is usually used in conjunction with the sort command in Linux systems to report or ignore duplicate lines in a file.

(2) the command syntax format is: uniq [option] parameter

(3) Common options include:

-c: count;-d: show only duplicate lines

-u: displays only rows that appear once

Example 1:uniq command to deduplicate, can only be repeated continuously, if separated, it will still appear.

If we want to remove all duplicate lines and only appear once, we can use the sort-u command

III. Wc tools

(1) count the number of bytes, words and lines in the specified file, and display the statistical results. This command counts the number of bytes, words, and lines in the specified file. If no file name is given, it is read from standard input. Wc also gives the presidential count of the specified file.

(2) format: wc [options] file

(3) Common options:

-c counts the number of bytes;-l counts the number of lines;-m counts the number of characters. Note that this flag cannot be used with the-c flag;-w counts the number of words; a word is defined as a string separated by white space, tabs, or newline characters;-L prints the length of the longest line.

Tags: Files sorting statistics commands tools examples words characters bytes commonly used formats contents parameters data flags results and output different longest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB OPPO Reno Huawei MySQL