Shell special symbol, cut, sort, WC, uniq, tee, TR, split command

Source: Internet
Author: User

* Represents 0 or more arbitrary characters

[[email protected] ~]# ls *txt

11.txt 1.txt 22.txt 2.txt aa.txt a.txt


? Represents only one arbitrary character

Whether it's a number or a letter, as long as it matches a single character.

[email protected] ~]# ls?. Txt

1.txt 2.txt a.txt B.txt

[[email protected] ~]# ls? txt

ls: unreachable? txt: no file or directory


Comment Symbol #, after which the contents of this symbol will be ignored

[Email protected] ~]# a=123 #456

[Email protected] ~]# echo $a

123


The caret character \, this character reverts the following special symbol (such as *) to normal characters.

[Email protected] ~]# ls-ld test*

Drwxr-xr-x. 2 root root 6 November 14:06 test

[Email protected] ~]# ls-ld test\*

LS: Unable to access test*: No file or directory


Pipe symbol | , which is the function of the output of the preceding command as input to the subsequent command. Not all commands can be used in pipe characters, common commands: Cat, less, head, tail, grep, cut, sort, WC, uniq, tr, split, SED, awk, and so on.

[email protected] ~]# Cat 1.txt | Wc-l

1


Cut command

The Cut command is used to intercept a field.

The format is: cut-d ' delimiter '-[fc]n,n is a number.

-D: followed by a delimiter, delimited by single quotation marks.

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

-C: followed by the first few characters

(-C does not need and-f,-d need-f)


[Email protected] ~]# head-5/etc/passwd | Cut-d ': '-f 1-4

root:x:0:0

Bin:x:1:1

Daemon:x:2:2

Adm:x:3:4

Lp:x:4:7

-D colon is delimiter, and-F 1-4 intercepts 1-4 segments


[Email protected] ~]# head-5/etc/passwd | Cut-c 1-4

Root

Bin

Daem

Adm:

Lp:x

The-C can be 1 digits or interval 1-4, or multiple numeric n1,n2,n3 (separated by commas).

[Email protected] ~]# head-5/etc/passwd | Cut-c 1,6,8

Rx0

B::

Dnx

A::

L47



Sort command

The sort command is used as sorting.

The format is: Sort-[-t delimiter] [-kn1,n2] [-nru] n1,n2 is a number.

-T: The following is the same as the split character, function and cut-d.

-N: Indicates the use of pure numeric sorting.

-r: Indicates reverse ordering.

-U: Indicates removal of duplicates.

-KN1,N2: Indicates n1-n2 interval ordering, or write-only-kn1.


If sort does not have any options, the first character is compared to the ASCII values one at a time, and finally the characters are output in ascending order.

[Email protected] ~]# Head-n 5/etc/passwd | Sort

a Dm:x:3:4:adm:/var/adm:/sbin/nologin

b In:x:1:1:bin:/bin:/sbin/nologin

D Aemon:x:2:2:daemon:/sbin:/sbin/nologin

L P:x:4:7:lp:/var/spool/lpd:/sbin/nologin

R Oot:x:0:0:root:/root:/bin/bash


The-t option is followed by a delimiter, and the-K option follows a number to sort the string for the first range, and the-N option means to sort using a pure number.

[email protected] ~]# head-n 5/etc/passwd | sort-t ': '-K 3-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


The-K option is followed by the number n1,n2 to sort the strings within the region of N1 and N2, and the-R reverses the sort.

[email protected] ~]# head-n 5/etc/passwd | sort-t ': '-K 3,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



WC command

The WC command is used to count the number of lines, characters, or words of a document.

-L count rows

-W Statistic Word number

-M statistic character number

WC does not follow any parameters directly with the document will be the number of rows, words, characters, sequentially output.

[Email protected] ~]# WC/ETC/PASSWD

1128/etc/passwd

[Email protected] ~]# wc-l/etc/passwd

24/etc/passwd

[Email protected] ~]# wc-w/etc/passwd

44/etc/passwd

[Email protected] ~]# wc-m/etc/passwd

1128/etc/passwd



Uniq command

The Uniq command is used to delete duplicate rows, common option-C, which represents the number of rows counted and writes the number of rows in front.

Before using Uniq, you have to sort the files first (sort), otherwise it doesn't work.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/17/855ac6ab2e1b7e8db6ad0f155bb02c2e.png "title=" Clipboard (1). png "alt=" 855ac6ab2e1b7e8db6ad0f155bb02c2e.png "/>


Tee command

Tee command after the root file name, acting similar to output redirection (>), it can also write to the following file, also displayed on the screen. Commonly used for pipe characters | After.

-A: Use Append mode when redirecting to files

650) this.width=650; "src=" Https://s5.51cto.com/oss/201711/17/3e236bf86520261a3bba15cb3b8c4e62.png "title=" Clipboard (5). png "alt=" 3e236bf86520261a3bba15cb3b8c4e62.png "/>

Use the-a parameter. Additional.

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/17/ad9ba12937b87690a89b473af8769537.png "title=" Clipboard (4). png "alt=" Ad9ba12937b87690a89b473af8769537.png "/>


TR command

The TR command is used to replace characters and is commonly used to handle special symbols appearing in documents.

-D means to delete a character.


[Email protected] ~]# echo "Huangdalinux" | Tr ' A ' a '

Huangdalinux #将字符串中的a替换成A

[Email protected] ~]# echo "Huangdalinux" | Tr ' an '

Huangdalinux #将字符串中的a和n替换成A和N

[Email protected] ~]# echo "Huangdalinux" | Tr ' A-Z ' A-Z

Huangdalinux #将字符串中的小写字母替换成大写字母


Delete a character in a string by using the-D parameter

[Email protected] ~]# echo "Huangdalinux" | Tr-d ' a '

Hungdlinux

[Email protected] ~]# echo "Huangdalinux" | Tr-d ' A,u '

Hngdlinx

S

Plit command

The split command is used to cut documents, and the common options are-B and-L

-B: Indicates that the document is split by size, and the default unit is byte.

650) this.width=650; "src=" Https://s1.51cto.com/oss/201711/17/6036ba9d73824195df9e8c7bed891411.png "title=" Clipboard (3). png "alt=" 6036ba9d73824195df9e8c7bed891411.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/17/6f31d9ae1d8988b865fe011d9b6bc64f.png "title=" Clipboard (2). png "alt=" 6f31d9ae1d8988b865fe011d9b6bc64f.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

-L: Indicates that the document is divided by the number of rows.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= " Https://s5.51cto.com/oss/201711/17/4df51084bee1ea5427abec3cdb16fe3b.png "title=" clipboard (6). png "alt=" 4df51084bee1ea5427abec3cdb16fe3b.png "/>

Specifies the destination file name.

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= " Https://s1.51cto.com/oss/201711/17/529a4e34f89593dd20803917b09f926a.png "title=" Clipboard.png "alt=" 529a4e34f89593dd20803917b09f926a.png "/>


The special symbol $, used as the identifier for the preceding variable.


Special symbols;. If you want to run two or more commands in a row, you need to add a sign between the commands.


Special symbol ~. Represents the user's home directory, the root user is/root, and the normal user is the/home/user name/


Special Symbols &. You can put a command in the background to execute. After the command is received.


Output redirect , output redirect Append >>, error redirect 2>, error redirect append 2>>, correct and error output both redirect &>. correct and error outputs both redirect append &>>


Brackets [], in parentheses, are character combinations that represent any one of the character combinations and can be a range.


Special symbols && and | |

Command1;command2

When used, the Command2 is executed regardless of whether or not the Command1 is performing successfully.

Command1&&command2

When && is used, Command2 is executed only if Command1 is executed successfully.

command1| | Command2

Use | | , Command1 execution succeeds, Command2 does not execute, otherwise execution command2, that is, Command1 and Command2 always have a command to execute.


This article is from the "Network Management Learning Road" blog, please be sure to keep this source http://754599082.blog.51cto.com/11510506/1982863

Shell special symbol, cut, sort, WC, uniq, tee, TR, split command

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.