Linux diary: Cut grep sort WC & uniq

Source: Internet
Author: User
Tags month name
Linux diary: Cut grep sort WC & uniq

After several days, I finally had time to write a Linux blog. Recently I felt a lot of emotion, but I was unable to give up my love for Linux. So I continued my research and didn't want to delay my step forward due to chores, I am literature and art.

Cut command.

The basic usage of cut is as follows:

Cut-D 'separator '-F Fields

Cut-C character range

Parameter description:-D: the delimiter that follows and is used with-f. -F: cut a piece of information into a book Segment Based on the delimiter of-D, and use-F to get the meaning of the segment;-C: Take character (characters) take out a fixed character range in units.

Instance:

1. Retrieve the 3rd paths of the PATH variable:

Echo $ PATH | cut-d': '-F 5

2. Extract the 3rd to 5 paths of the PATH variable:

Echo $ PATH | cut-d': '-F 3-5

3. Retrieve the 3rd and 5th paths of the PATH variable:

Echo $ PATH | cut-d': '-F 3,5

4. Cut off the first 12 characters of each row in the output result of the export:

Export | cut-C 12-

Grep command

Grep's advanced usage of regular expressions is not involved here.

The basic usage is as follows:

Grep [-acinv] [-- color = auto] 'query string' filename

-A: Search for data from binary files in TXT files.

-C: calculates the number of times a 'query string' is found.

-I: case insensitive

-N: Output row number

-V: reverse selection, that is, the row without the 'search string' content is displayed.

-- Color = Auto: You can add the color of the keywords to display them. Generally, the operating system uses aliasgrep = 'grep-color = auto' by default'

Instance:

1. Find the row where reboot is located in last:

Last | grep 'reboot'

2. Reverse Selection:

Last | grep-V 'reboot'

Sort command

Sort [-fbmnrtuk]

-F: Ignore the case sensitivity differences. This is weird, not-I. It is found after you find the help: foldlower case to upper case characters.

-B: Ignore the leading space character.

-M: sort by month name

-N: Use "pure numbers" for sorting

-R: reverse sorting

-U: uniq. Only one row is displayed in the same data.

-T: separator, Tab by default

-K: The range in which the data is sorted.

Instance:

1. Sorting of personal accounts:

CAT/etc/passwd | sort

2. sort by the third column:

CAT/etc/passwd | sort-t': '-K 3

3. sort by numbers in three columns:

CAT/etc/passwd | sort-t': '-K 3-N

4. truncate the last Output account and sort it:

Last | cut-D ''-F 1 | sort

WC command

Usage:

WC [-lwn]

-L: only list rows.

-W: How many words are listed (English words)

-N: the number of characters.

Instance:

1. Calculate the number of characters in/etc/manpath. config.

CAT/etc/manpath. config | WC

2. calculate the total number of Logon systems in this month:

Last | grep [A-Za-Z] | grep-V 'wtmp' | WC-l

Uniq command

Used to display duplicate data only once.

Uniq [-ic]

-I: case insensitive

-C: Count

Instance:

1. Use last to list accounts. Only the account column is retrieved. After sorting, only one digit is output.

Last | cut-D ''-F 1 | sort | uniq

2. Calculate the number of logins:

Last | cut-D ''-F 1 | sort | uniq-C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.