Text processing commands

Source: Internet
Author: User

I. # WC: print the number of newlines, words, and bytes in files

WC [Option] [file]

-L: Statistical line

-C: counts the number of bytes.

-W: Number of words

For example: A. count the number of users in the current system: WC-L/etc/passwd

B. Count the files in/bin: ls-L/bin | WC-l

C. count the number of files starting with P in the/etc directory: ls-D/etc/[PP] * | WC-l

2. # TR: Convert or delete characters in translate, and use pipelines |


For example, a, CAT/etc/issue | tr 'a-Z': converts letters in issue to uppercase letters.


B. Echo "Hello World" | tr 'abcdefg': Convert the abcdefg contained in Hello World

Convert to uppercase.


C. Echo "Hello World" | tr-D 'abcdefg': Delete the characters in "Hello World" that contain abcdefg.


D, head-7/etc/passwd | tail-1 | cut-D:-F1 | tr 'a-z'' A-Z ': retrieve the username in line 1 of the/etc/passwd file and convert it to uppercase characters

3. # Cut: Cut, separated and displayed based on the specified separator.

Echo "this is a new line." | cut-d'-F1

Cut-D:-F1/etc/passwd = CAT/etc/passwd | cut-D:-F1 show all user names

CAT/etc/passwd | cut-D:-F1, 7 show all users and their shells

Head-7/etc/passwd | tail-1 | cut-D:-F1, retrieve the username in row 7th of the/etc/passwd file

Iv. # sort: sort the output file content.

Sort [Option] File

-F: Case Insensitive characters.

-N: Compare the content by numerical value.

-T: Specifies the delimiter.

-K: Specifies the comparison field after segmentation

-U: duplicate rows (repeated rows are consecutive rows), only once.

For example:

A, sort/etc/fstab

B. CAT/etc/passwd | cut-D:-F3 | sort-N = cut-D:-F3/etc/passwd | sort-N sorts the IDs of all users.

C. Cut-D:-F7/etc/passwd | sort-u displays the shells of all users on the current system, and the shell in the cup is only displayed once.

V. # uniq: Remove duplicate rows (duplicate rows are continuous rows)

-C: count the number of duplicates in each row.

-D: only duplicate rows are displayed.

-U: displays only non-duplicate rows.

6. head and tail:

Extract the 7th rows of the/etc/passwd file: Head-7/etc/passwd | tail-1


Text processing commands

Related Article

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.