Introduction to the cut command for viewing files and content processing in linux, linuxcut
1. Command description:
The cut command separates and outputs each line of a file by a specified delimiter.
2. Syntax:
Cut [Option]... [file list]...
3. instance:
For example, in the file/etc/passwd, each line contains seven columns of text separated by six colons:
[Root @ CentOS ~] # Cat/etc/passwdroot: x: 0: 0: root:/bin/bashbin: x: 1: 1: bin:/sbin/nologindaemon: x: 2: 2: daemon:/sbin/nologinadm: x: 3: 4: adm:/var/adm:/sbin/nologinlp: x: 4: 7: lp:/var/spool/lpd:/sbin/nologinsync: x: 5: 0: sync:/sbin:/bin/syncshutdown: x: 6: 0: shutdown: /sbin:/sbin/shutdownhalt: x: 7: 0: halt:/sbin/halt ...... (skip the content )......
If we want to extract specific information, for example, we need to print out all users in the system:
[Root @ CentOS ~] # Cat/etc/passwd | cut-f 1-d ':'???? # The-f option specifies the column, which indicates that the first column is extracted. The-d option specifies the rootbindaemonadmlpsyncshutdownhalt ...... (Omitted )......
Or print out the home directory of the user and the user at the same time:
[Root @ CentOS ~] # Cat/etc/passwd | cut-f 1,6-d': 'root:/rootbin:/bindaemon:/sbinadm:/var/admlp:/var/spool/lpdsync: /sbinshutdown:/sbinhalt:/sbin ..... (skip the content )......
Or print the logon shell (that is, the shell used for user logon) for each user at the same time ):
[Root @ CentOS ~] # Cat/etc/passwd | cut-f 1,6-7-d ': 'root:/root:/bin/bashbin:/bin:/sbin/nologindaemon:/sbin: /sbin/nologinadm:/var/adm:/sbin/nologinlp:/var/spool/lpd:/sbin/nologinsync:/sbin:/bin/syncshutdown:/sbin: /sbin/shutdownhalt:/sbin/halt ...... (skip the content )......
We can find that the preceding cut use scenario has a special feature, that is, there are specific delimiters in the row to be processed, but if the row to be processed has no delimiters, is cut useless? The answer is no. Suppose we want to print 1st ~ 5 characters, and 7th ~ The 10 characters are as follows:
[Root @ CentOS ~] # Cat/etc/passwd | cut-c 1-5, 7-10 ???? # Where the-c option indicates that the root: 0: 0bin: x1: 1: daemo: x: 2adm: x3: 4: lp: x: 7: lsync: 5: 0 shutdwn: xhalt: 7: 0 ...... (skip the content )......