Cut command
Cut is a character selection command that cuts bytes, characters, and fields from each line of a file and converts those bytes, characters, and fields to standard output.
The syntax format is:
Cut [BCDFN] ... file ...
Main parameters
-B: Split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
-C: Split in characters.
-D: Custom delimiter, default is tab.
-F: Used with-D to specify which area to display.
-N: Cancels splitting multibyte characters. Used only with the-B flag.
Example 1: Remove the user name from the/etc/passwd and Shell is/bin/bash
# CAT/ETC/PASSWD | grep '/bin/bash ' | Cut-d:-f1,7root:/bin/bashxc:/bin/bashmysql:/bin/bashnagios:/bin/bashxcroom-01:/bin/bash
-b: Intercept in bytes
[[email protected] test]# whoroot tty1 2015-07-30 17:59root pts/3 2015-08-24 00:52 (192.168.0.105) root pts/ 4 2015-08-24 01:11 (192.168.0.105) [[email Protected] test]# who | cut -b 4ttt[[email protected] test]# who | cut -b -3roorooroo[[email protected] test]# who | cut -b 3-ot tty1 2015-07-30 17:59ot pts/3 2015-08-24 00:52 (192.168.0.105) ot pts/4 2015-08-24 01:11&nBSP; (192.168.0.105) [[email protected] test]# who | cut -b -3,3-root tty1 2015-07-30 17:59root pts/3 2015-08-24 00:52 (192.168.0.105) root pts/4 2015-08-24 01:11 (192.168.0.105)
-C: positional identification by character
[[email protected] test]# Cat xinqi.txt Monday Tuesday Wednesday Thursday
-B: In bytes, support for multibyte characters is not good, output is not, blank
[email protected] test]# Cat Xinqi.txt | Cut-b 3
-C: Multi-byte characters can be supported
[email protected] test]# Cat Xinqi.txt | Cut-c 31,234
-N: Used to tell the cut not to disassemble the multi-byte, combined with B
[email protected] test]# Cat Xinqi.txt | CUT-NB 3 star Stars [[email protected] test]# Cat Xinqi.txt | CUT-NB star Stars [[email protected] test]# Cat Xinqi.txt | CUT-NC Thursday Tuesday Wednesday Thursday [[email protected] test]# Cat Xinqi.txt | CUT-NC Phase 2 biennium
-F: Specify an interval character
For those non-fixed format information,-C, and-B are not good,
The Cut command provides a way to extract this, specifically, by setting the spacer.
[Email protected] test]# CAT/ETC/PASSWD | grep '/bin/bash ' | Cut-d:-f1-3root:x:0xc:x:500mysql:x:27nagios:x:501xcroom-01:x:502[[email protected] test]# cat/etc/passwd | grep '/bin/bash ' | Cut-d:-f1-3,7root:x:0:/bin/bashxc:x:500:/bin/bashmysql:x:27:/bin/bashnagios:x:501:/bin/bashxcroom-01:x:502:/ Bin/bash[[email protected] test]# cat/etc/passwd | grep '/bin/bash ' | Cut-d:-f-3root:x:0xc:x:500mysql:x:27nagios:x:501xcroom-01:x:502
What to do if you encounter a space
Sed-n-i/path/to/filename: You can see if the text is at the end of the
[email protected] test]# cat 1.txthello good morningyeswho is you @[[email protected] test]# sed-n L 1.txthello $ $good Morning$yes$who is you @$
To intercept spaces:
[email protected] test]# Cat 1.txt | Cut-d '-f1hellogoodyeswho
Note: Two single quotation marks can be really a space, after-D can only add a space, do not add more, because cut only allows the spacer is a character.
Reference: http://www.cnblogs.com/dong008259/archive/2011/12/09/2282679.html
This article is from the "Small City Studio" blog, please be sure to keep this source http://xcroom.blog.51cto.com/7941996/1687774
The Linux Cut command