8.10 Shell Special Symbol cut command 8.11 sort_wc_uniq command 8.12 tee_t

Source: Internet
Author: User

8.10 Shell Special Symbol Cut command

Command: Cut

Used to intercept a field

Syntax: cut-d ' delimited character ' [-CF] n Here's n is the number

-D: followed by delimited characters, separated by single quotation marks

-C: followed by the first few characters

-F: The next section is followed by the number of blocks

[[email protected] ~]# cat/etc/passwd |cut-d ': '-F 1 |head-n5
Root
Bin
Daemon
cd/
Lp
-D followed by the delimiter character, where a colon is used as the split character, and-F 1 is the first paragraph, and the space between-F and 1 is optional.

[Email protected] ~]# HEAD-N2/ETC/PASSWD|CUT-C2
O
I
[Email protected] ~]# HEAD-N2/ETC/PASSWD|CUT-C1
R
B
[Email protected] ~]# head-n2/etc/passwd|cut-c1-10
root:x:0:0
Bin:x:1:1:
[Email protected] ~]# head-n2/etc/passwd|cut-c5-10
: x:0:0
X:1:1:
The-C can be either 1 digits n or an interval n1-n2, or multiple digits n1,n2,n3

[Email protected] ~]# head-n2/etc/passwd|cut-c1,3,10
Ro0
Bn

8.11 Sort_wc_uniq Command

Command: Sort

Sort for sorting

Syntax: sort [-t delimiter] [-KN1,N2] [-NRU] Here's N1 < N2

-T delimiter: function with the-D one meaning of cut

-N: Sorting with pure numbers

-R: Reverse Sort

-U: To repeat

-KN1,N2: Sorted by N1 interval to N2 interval, can only write-kn1, sort N1 field

[Email protected] ~]# head-n5/etc/passwd |sort
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Root:x:0:0:root:/root:/bin/bash
If sort does not have any options, then the first character is backward, then the ASCII values are compared, and finally they are output in ascending order.

[Email protected] ~]# head-n5/etc/passwd |sort-t:-k3-n
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-T followed by a delimiter,-K followed by a number, a string that is sorted on the first range, and-n indicates the use of a purely numeric sort

[Email protected] ~]# head-n5/etc/passwd |sort-t:-k3,5-r
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Bin:x:1:1:bin:/bin:/sbin/nologin
Root:x:0:0:root:/root:/bin/bash
-k3,5 represents a string sort from 3rd to 5th,-R for reverse ordering


An error occurred because T: there is no space behind it.

Command: WC

The number of rows, characters, and words used to count the document, and the commonly used options are:

-L: Count rows

-M: Statistics of characters

-W: Number of statistical words

[Email protected] ~]# WC/ETC/PASSWD
Panax Notoginseng 1220/etc/passwd
[Email protected] ~]# wc-l/etc/passwd
27/etc/passwd
[Email protected] ~]# wc-m/etc/passwd
1220/etc/passwd
[Email protected] ~]# wc-w/etc/passwd
37/etc/passwd
WC does not follow any options, directly with the document, it will be the number of rows, words, characters in turn output.


Command: Uniq

To repeat the line, the most common option for Amin is only one:

-C: Count the number of repeated rows and write the number of lines in front

[Email protected] ~]# vim Testb.txt
Write the following content into Testb.txt, save.

111
222
111
333
The premise of using Uniq is to sort the files first, otherwise it doesn't work.

[Email protected] ~]# Uniq testb.txt
111
222
111
333
[Email protected] ~]# sort Testb.txt |uniq
111
222
333
[Email protected] ~]# sort Testb.txt |uniq-c
2 111
1 222
1 333

8.12 tee_tr_split Command

Command: Tee

followed by the file name, similar to the redirect ">", but the specific weighting is a feature that is also displayed on the screen while writing the file to the file that follows it.

[Email protected] ~]# echo "AAAAAAAAAAAAAAAAAAAAAAAAAAA" |tee testb.txt
Aaaaaaaaaaaaaaaaaaaaaaaaaaa
[email protected] ~]# cat Testb.txt
Aaaaaaaaaaaaaaaaaaaaaaaaaaa
Tee common Word pipe "|" after.

Command: TR

Substitution characters, commonly used to handle special symbols appearing in a document, such as ^m symbols appearing in a DOS document. There are two common options:

-D: Delete a character, followed by the character you want to delete

-S: Remove repeated characters

The most common is to capitalize the lowercase: tr ' [A-z] ' [A-z] '

[[email protected] ~]# head-n2/etc/passwd |tr ' [A-z] ' [A-z] '
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Of course, replacing a character is also possible.

[[email protected] ~]# grep ' root '/etc/passwd |tr ' r ' R '
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
However, the substitution, deletion and repetition are all aimed at one character and have some limitations. If it's not working for a string, Amin recommends that you simply understand the TR, and you'll learn more about the tools you can implement for string manipulation.


Command: Split

Cutting documents, common options:

-B: Divide the document by size in bytes

[Email protected] ~]# mkdir Split_dir
[Email protected] ~]# CD!$
CD Split_dir
[Email protected] split_dir]# cp/etc/passwd.
[Email protected] split_dir]# split-b500 passwd
[[email protected] split_dir]# ls
passwd XAA Xab Xac
If split does not specify a target file name, it will be Xaa xab ... Such a filename to access the cut file. Of course we can also specify the target file name:

[Email protected] split_dir]# split-b500 passwd 123
[[email protected] split_dir]# ls
123AA 123ab 123ac passwd
-L: Split document by number of rows

8.13 Shell Special Symbol

$ In addition to the identifier used in front of the variable, there is also a magic, that is, and '! ' used together.
[[email protected] ~]# ls testb.txt
Testb.txt
[[email protected] ~]# ls!$
LS Testb.txt
Testb.txt
'!$ ' represents the last variable in the previous hit (in short, the last thing that appears in the previous command) For example, the top command is finally Testb.txt, so entering!$ under the current command represents Testb.txt.

; Semicolon Usually we are in a row to hit a command, and then enter the run, so you want to run two or two more commands in a row? You need to add a ";" between the commands.
[[email protected] ~]# ls-d test; touch test111; ls-d Test
Test test1 test2 test3 Testa Testb.txt
Test test1 test111 test2 test3 Testa Testb.txt
~: User's home directory, if Root is/root, ordinary user is/home/username
[Email protected] ~]# CD ~
[Email protected] ~]# pwd
/root
[email protected] ~]# su test
[Email protected] root]$ CD ~
[Email protected] ~]$ pwd
/home/test

&: If you want to put a command in the background, you need to add this symbol. This is typically used when the command runs for a very long time.
[Email protected] ~]# sleep &
[1] 3260
[[email protected] ~]# jobs
[1]+ Running Sleep &

, >>, 2>, 2>> in front of the directional symbols > and >> the meaning of substitution and append, and then there are two symbols that are here 2> and 2>> respectively for error redirection and error append redirection, When we run a command error, the error message will be output to the current screen, if you want to redirect to a text, you need to use 2> or 2>>
[[email protected] ~]# ls AAAA
LS: Unable to access AAAA: No file or directory
[1]+ Done Sleep 30
[[email protected] ~]# ls AAAA
LS: Unable to access AAAA: No file or directory
[email protected] ~]# ls AAAA 2>/tmp/error
[Email protected] ~]# Cat/tmp/error
LS: Unable to access AAAA: No file or directory
[email protected] ~]# ls AAAA 2>>/tmp/error
[Email protected] ~]# Cat/tmp/error
LS: Unable to access AAAA: No file or directory
LS: Unable to access AAAA: No file or directory
[] in brackets, the middle is a combination of characters, which represents either of the intermediate characters.
[Email protected] ~]# ls-d test*
Test test1 test111 test2 test3 Testa Testb.txt
[Email protected] ~]# ls-d Test[1-3]
Test1 test2 Test3
[Email protected] ~]# ls-d TEST[1A3]
Test1 Test3 Testa
[Email protected] ~]# ls-d test[0-9]
Test1 test2 Test3
[Email protected] ~]# ls-d test[0-9a-z]
Test1 test2 test3 Testa
&& | |
A semicolon has just been mentioned above for the delimiter between multiple commands. There are also two special symbols that can be used in the middle of multiple commands, that is, "&&" and "| |" Below are all listed in the following Amin:

Command1; Command2
Command1 && Command2
Command1 | | Command2
When using ";", Command2 will be executed regardless of whether Command1 is executed successfully;

When using "&&", only if the Command1 is executed successfully, Command2 will execute, otherwise command2 not execute;

When using "| |", the Command1 execution succeeds Command2 not execute, otherwise go to execute Command2, in short Command1 and Command2 always have a command to execute.

Before doing the experiment, Amin want to remove all the test*, but the deletion, but the prompt said that the authority is not enough, the following is the process of eliminating the problem Amin:

[Email protected] ~]# RM-RF test
RM: Unable to delete "Test2/test1": Insufficient permissions
RM: Unable to delete "Test2/test3": Insufficient permissions
RM: Unable to delete "Test2/test4": Insufficient permissions
[[email protected] ~]# ls test

Test1 Test3 test4
[Email protected] ~]# lsattr test
-----a-------E-test2/test1
----I--------E-test2/test3
-------------E-test2/test4
[Email protected] ~]# chattr-a test2/test1
[Email protected] ~]# chattr-i test2/test3
[Email protected] ~]# RM-RF test

RM: Unable to delete "Test2/test1": Insufficient permissions
RM: Unable to delete "Test2/test3": Insufficient permissions
RM: Unable to delete "Test2/test4": Insufficient permissions
[[email protected] ~]# ls test
Test1 Test3 test4
[Email protected] ~]# LS-LD test

Drwxrwxr-x 2 root root 4096 May 10:12 test2
[Email protected] ~]# ls-l test2/
-rw-r--r--1 root root 6 May 10:20 Test2/test1
-rw-r--r--1 root root 0 May 10:11 test2/test3
-rw-r--r--1 root root 0 May 10:12 test2/test4
[Email protected] ~]# lsattr test2/

-------------E-test2/test1
-------------E-test2/test3
-------------E-test2/test4
[Email protected] ~]# lsattr test2
-------------E-test2/test1
-------------E-test2/test3
-------------E-test2/test4
[Email protected] ~]# lsattr-d test2
----I--------E-test2
[Email protected] ~]# chattr-i test2/
[Email protected] ~]# RM-RF test2/
If you have done the same experiment with Amin before, I believe you will have the same problem. Next, Amin to explain the effects of the two special symbols "&&" and "| |" By doing experiments:

[email protected] ~]# Touch test1 test3
[[email protected] ~]# ls test2 && touch test2
LS: Unable to access test2: No file or directory
[[email protected] ~]# ls test2
LS: Unable to access test2: No file or directory
[[email protected] ~]# ls test2 | | Touch Test2
LS: Unable to access test2: No file or directory
[[email protected] ~]# ls test*
Test1 test2 Test3

8.10 Shell Special Symbol cut command 8.11 sort_wc_uniq command 8.12 tee_t

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.