Linux Command Learning Note: Cut details

Source: Internet
Author: User

The Cut command cuts bytes, characters, and fields from each line of the file and writes them to standard output.  It takes each line of the file as a processing object. Command format:Cut[options] [scope] file. option is used to specify the unit (byte, character, or field), and the range specifies the position of the option. The common options are:-B: Specifies that the selection is displayed in bytes-C: Specifies that the selection is displayed in character units-F: Specifies that the selection is displayed in a field-D: Specifies the delimiter for the field, and the default is tab other options and-N (used only with the-B option),-s (Only and-The f option is used together) the range represents: N: Nth byteCharactersField N-: From nth byte/character/field to last byte/character/Field N-M: from nth byte/character/field to M bytes/character/fields, including M-M: From the beginning of the line to the first m bytes/character/fields, including M-: Displays the user name, password, user ID, and user directory in the passwd file from the beginning of the line to the last line. The passwd file is divided into seven columns per line to: delimiter; The seven columns in turn are user name, password, user ID, group ID, user description, user directory, user bash. Due to the excessive content of the passwd file, we use sed here to display 1 of these-6 lines. Second, display the first two bytes or the first two characters appear-B option and-The C option is no different. In fact, if the document content has Chinese characters can see the difference between the two. Three, you are not a little puzzled: Chinese characters do not account for two bytes? Cut-B1-2Test1 just shows two bytes. Yes, there's a lot of information on the internet that says so, and they'll tell you to use Cut-b1-3 can be a complete display of a Chinese character, the reason is Cut-b1-2 is processed in bytes, and the characters are "cut in half" and therefore not displayed properly. But look at the following picture, you may have different views: hehe. I'm really sorry, it looks like UTF-8 to GB2312 encoding conversion did not succeed, but it doesn't matter, it doesn't affect us on why Cut-b1-2 The judgment of Chinese characters cannot be displayed correctly. This picture will show youThe difference between-B and-C options: we use Cut-b1-2Test1, no Chinese characters are displayed, but with the-C option you can display two characters. This is the-B (bytes) and-the difference between C (characters). We see the code for UTF-8 test1 File The first line has 5 characters and 3 letters, but the wc-c statistic is 19 bytes, and the test file encoded as iso-8859 counts as 14 bytes! (WC-C in Statistics byte, Terminator/The newline character also acts as a byte. This indicates: in UTF-8 encoding, Chinese characters are accounted for three bytes! You can try it yourself, in UTF.-8 code, with Cut-b1-3 can display a Chinese character, and in other codes such as gb2312, iso-8859 and other codes, with the Cut-b1-2 You can display a Chinese character. (b), I. Simply learn to create a new file, named names, with the following $Catnames Farseer Tony Emanuel Lucy Jim Chenjihuang Jack If we're going to extract the 3rd character, do it.Cut-C3 names R n a c m e c if you want to extract the 3rd, 5 characters, so, separated by commas, if not the $Cut-C3,5names re n au c m eJ c if it is 2 to 4 characters, use the connection number "-" $Cut-c2-4names Ars onyMansUcy im hen ack oneself feel understanding "," and "-" function, think about if you remove these symbols, the above example will output what. Understand, you have to try to have a deeper understanding!! Two. Filter action filter, do not need me to speak also understand, is to filter the things we do not want, the rest of us. Maybe you've seen such a command.CatNames | LessIf you don't understand"|"What is it, you first remember that this is a way of writing, and "|" This is called the pipeline, and then there will be an explanation in order to reflect the role of the filter, we first use the WHO (the previous presentation) to see the $W.H.O.Farseer Tty7 --Geneva- -  A: +(:0) Farseer pts/0         --Geneva- -  -: on(:0.0) User Tty9 --Geneva- -  A: the(: -If you just want the user name, it is not very uncomfortable now, the back of the time is really unnecessary trouble, when cut on the effect of the $W.H.O.|Cut-c1-7Farseer Farseer User The longest name Farseer also only 7 characters, the first 7 to take out, short also followed out, the extra is just a space, does not affect reading. Of course, after coming out you can also sort $W.H.O.|Cut-c1-7|SortIf you want to show what terminal (second column content), that's the same reason $W.H.O.|Cut-c10- -tty7 pts/0Tty9 by the way, look at the effect $W.H.O.|Cut-c10- -|Sortpts/0Tty7 Tty9 If I just want the user name and date and what's behind the date? It is also very simple, in {A. Simple understanding} has been mentioned in the role of "," now strengthen the understanding of the $W.H.O.|Cut-c1-7, A-Farseer --Geneva- -  A: +(:0) Farseer --Geneva- -  -: on(:0.0) User --Geneva- -  A: the(: -) If you put 1-7, A-swap a little bit? $W.H.O.|Cut-c22-,1-7Farseer --Geneva- -  A: +(:0) Farseer --Geneva- -  -: on(:0.0) User --Geneva- -  A: the(: -visible, or in the order of the original. As for these figures,17 is how to come, very simple, own number, or try to see the result is OK. Here, you should have a deeper understanding of the cut command, but it's not over yet. Three.-D and-f option to know that our user information is stored in the/etc/passwd This file inside, let's take a look at the first 10 lines $Head/etc/passwd#head就是显示文件前十行的命令 root:x:0:0: root:/root:/bin/Bash daemon:x:1:1:d aemon:/usr/sbin:/bin/SHbin:x:2:2: bin:/bin:/bin/SHsys:x:3:3: sys:/dev:/bin/SH    Sync: x:4:65534:Sync:/bin:/bin/Syncgames:x:5: -: games:/usr/games:/bin/SH    Mans: x:6: A:Mans:/var/cache/Mans:/bin/SH    LP: x:7:7:LP:/var/spool/lpd:/bin/SHmail:x:8:8: mail:/var/mail:/bin/SHnews:x:9:9: news:/var/spool/news:/bin/SHif we were to remove the username, it would be a bit difficult. Because there are a lot of extra characters that follow the previous method. SoThe-D and-f options are handy. Because the discovery has a characteristic, these content is used":"separated from each other. Format:Cut-ddchar-ffieldsfileNote: Dchar is a delimited symbol, Fileds is the segment number after separation see the following example is almost understood to facilitate operation, we wrote the first 10 lines in a text called pass, using output redirection">"is to output the contents of the screen to the specified file, if the file does not exist automatically created,">"This symbol is very image $Head/etc/passwd>Pass $CatPass ROOT:X:0:0: root:/root:/bin/Bash daemon:x:1:1:d aemon:/usr/sbin:/bin/SHbin:x:2:2: bin:/bin:/bin/SHsys:x:3:3: sys:/dev:/bin/SH    Sync: x:4:65534:Sync:/bin:/bin/Syncgames:x:5: -: games:/usr/games:/bin/SH    Mans: x:6: A:Mans:/var/cache/Mans:/bin/SH    LP: x:7:7:LP:/var/spool/lpd:/bin/SHmail:x:8:8: mail:/var/mail:/bin/SHnews:x:9:9: news:/var/spool/news:/bin/SHOK, here's the real example. $Cut-D:-F1 pass root daemon bin sysSync GamesMans    LPMail News See,: Is the separation of symbols, 1 is the first paragraph after the separation, that is, the user name this time you should produce a thinking, if you want to break the nth, you only need to modify the parameters of F. The following sentence shows the user name and the path of the shell (the last paragraph of the content) $Cut-D:-f1,7Pass Root:/bin/Bash Daemon:/bin/SHBin:/bin/SHSYS:/bin/SH    Sync:/bin/SyncGames :/bin/SH    Mans:/bin/SH    LP:/bin/SHMail:/bin/SHNews:/bin/SHonce again see "," the function of the same "-" is also available, I do not give an example, you try it yourself. One more example, add a file QQ inside the store your friend's QQ, the contents are as follows $CatQQ Farseer619932509Chen Jihuang619932509the name and QQ number are separated by tabs (Tab key). Use the following command to view (This command is not overly investigated, I am not sure) $sed-n L QQ farseer\t619932509$ Chen jihuang\t619932509$ See, in fact, if you used C or C++, just know \ t escapes to tabs at this time you execute $Cut-F1 QQ Farseer Chen Jihuang found no use-d option, it can be segmented, why? Because the default separator symbol is a tab character. 

Linux Command Learning Note: Cut details

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.