Get the App
SLTechnology News&Howtos  ›  Servers  › 

Grep, cut, seq, sort, wc, uniq in Linux advanced command

Shulou Source: shulou.com Published: 2022-06-02 11:57:49 10月03日 Update

Grep

Grep command: matches the keyword, that is, displays the entire line of the keyword in the file.

Show all lines in the / etc/passwd file that contain the root keyword: grep root / etc/passwd

Displays all lines in the / etc/passwd file that contain the root keyword and shows which line the keyword is in the text:

Grep-n root / etc/passwd

-n display line number-v inverted

-I is case-insensitive-c the number of lines with keywords

-Q execute silently, that is, the matching content is not displayed on the screen

-A shows a few lines after the keyword

Grep-A 2u1 / etc/passwd shows the line where U1 is located, and the next two lines

-B to show the lines before the keyword

Grep-B 2u1 / etc/passwd shows the line of U1, and the previous two lines

-- color displays keywords with color

Grep root / etc/passwd-- color

Cut

Cut command: extract the fields (columns) and character contents specified in the contents of the file. The default delimiter for a word is a space.

-b: according to byte cutting, each Chinese character occupies 3 bytes in utf-8 coding

-c: cut by the character-d: specify the delimiter

-f: specify which field is separated by the delimiter

Example: cut-d:-f 1J 6 / etc/passwd

Displays the contents of columns 1 and 6 split by: in the file

Cut-d:-f 1Magol 3-5 / etc/passwd displays 5 columns of content in the file with: segmented 1pr 3pr 4 Jol.

Cut-d:-f-3 / etc/passwd

Display the first three columns in the file

Cut-d:-f 3-/ etc/passwd

Displays the contents of the third to last columns of the file

Cut-c 1-3 / etc/passwd take the first three characters of each line

Echo 'Welcome' | cut-b 1-6 take the first six bytes of 'Welcome'

Seq

Seq: print some numbers in order

Basic usage: seq [options]... Mantissa

Seq [options]... First and Mantissa

Seq [options]... First increment Mantissa

Example: seq 1 2 10 outputs odd numbers between 1 and 10

Seq 2 2 10 outputs even numbers between 1 and 10

Seq 10-2 1 outputs even numbers between 10 and 1

The same effect can be achieved with echo

Echo {first count. Mantissa. Increment}

Echo {1... 10} outputs 1 to 10 integers

Echo {1... 10... 2} outputs odd numbers from 1 to 10.

Echo {2... 10... 2} outputs an even number between 1 and 10.

Echo {10... 1... Mel 2} outputs even numbers between 10 and 1.

Sort

Sort sort (ascending, descending) default ascending order

-n: (numeric-sort numeric sort) default single character comparison based on numeric comparison

-r:reverse (reverse) output results in reverse order

-t: specify the delimiter (separator separator)

-k: sort build (key, sorted column field)

-u: remove duplicate lines (uniq unique)

Example: create a.txt with cat command

Sort-n-r-t:-k 3 a.txt

Output in reverse order by: partitioned third column

Sort-u a.txt

Remove duplicate lines from the a.txt file

Wc

Wc counts lines, words and bytes

-l: count the number of rows

-c: count the number of bytes

-w: count the number of words

-m: count the number of characters

Example: wc-l / etc/passwd statistics on the number of lines in a file

Uniq

Uniq merges consecutive duplicate rows

-u: only lines that do not have consecutive repeats are displayed

-c: count the number of consecutive repeated rows

-d: displays only one row that repeats continuously

-I: ignore case

Example: cat > a.txt

Tags: File key keyword content output statistics between character byte even delimiter Mantissa sort command word field place one line ascending order increment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Shulou Information Apple MySQL