Shell cut command usage

Source: Internet
Author: User

Similar to awk Functions

Example 1

 
$ A = 'echo root: X: 0: 0: Root:/root:/bin/bash | cut-D: -F 1,5 'shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aroot: Root

In the preceding example, the root: X: 0: 0: Root:/root:/bin/bash command is redirected to the cut command.-D indicates the delimiter. Here, the colon is used: as the separator,-F indicates the field, and 1st and 5th fields are selected,

Example 2: print only the first field

 
$ A = 'echo root: X: 0: 0: Root:/root:/bin/bash | cut-D: -F 1 'shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aroot

Example 3: print all fields after the first field, including the first field

Shuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root:/bin/bash | cut-D: -F 1-'shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aroot: X: 0: 0: Root:/root: /bin/bashshuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root:/bin/bash | cut-D: -F 3-'// print all fields following the 3rd fields, including the Third Field shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ A0: 0: Root: /root:/bin/bash

Example 4: capture 2nd to 4th Fields

 
Shuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root:/bin/bash | cut-D: -F 2-4 'shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ ax: 0: 0

Example 5: truncates a specified number of characters

Shuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root: /bin/bash | cut-C 2-5 '// intercept the string shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aoot: shuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root: /bin/bash | cut-C 2-7' // intercept the string shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aoot: X: shuohailhl @ shuohailhl-PC/cygdrive/d $ A = 'echo root: X: 0: 0: Root:/root: /bin/bash | cut-C-2 '// capture the first two characters shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aroshuohailhl @ shuohailhl-PC/cygdrive/d $ = 'echo root: x: 0: 0: Root:/root: /bin/bash | cut-C 2-'// capture 2nd shuohailhl @ shuohailhl-PC/cygdrive/d $ echo $ aoot: X: 0: 0: Root: /root:/bin/bash

Example 6 specify the file. The last parameter is the file name.

$ Cat pass.txt root: X: 0: 0: Root:/root:/bin/bashbin: X: 1: 1: Bin:/bin:/sbin/nologindaemon: X: 2: 2: daemon:/sbin/nologinadm: X: 3: 4: ADM:/var/adm: /sbin/nologinshuohailhl @ shuohailhl-PC/cygdrive/d $ cut-D:-F 1-3. /pass.txt root: X: 0bin: X: 1 daemon: X: 2adm: X: 3

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.