Special symbols and Cut,sort,wc,uniq,tee,tr,split commands in the shell

Source: Internet
Author: User


I. Special symbols in the shell


While you are learning Linux, you may have been exposed to a special symbol, such as "*", which is a wildcard symbol that represents 0 or more characters or numbers.
Special characters that are commonly used:
1, * represents 0 or more arbitrary characters.

2,? Represents only one arbitrary character, whether it is a number or a letter, as long as it matches one.

3, #这个符号在linux中表示注释说明的意思, that is, the content behind the # Linux ignored.

4, \ de-ideographic characters, the following special symbols (such as "*") revert to ordinary characters.

5, | The pipe character, which appears several times before, is the function of dropping the result of the preceding command to the command following the symbol. The following commands, which are mentioned here, are not available for all commands, and are commonly used for document operations such as Cat, less, head, tail, grep, cut, sort, WC, uniq, tee, tr, split, SED, awk, and so on, where G Rep, sed, awk is a tool that must be mastered for regular expressions.

The Wc-l command is used to calculate how many rows a document has.



Cut command
Used to intercept a field.
Grammar:


Cut - D 'separator' [- CF] / / N where n is a 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





Sort command
The sort command is very useful in Linux, which sorts the files and outputs the sort results standard. The sort command can get input from either a specific file or from stdin.
Grammar:


sort [- t separator] [- kn1, N2] [- NRU] / / N1 < N2 here

-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


Instance:
1, if the sort does not have any option, then from the first character backward, followed by the ASCII value of the comparison, and finally they are output in ascending order.

2,-t followed by a delimiter,-K followed by a number, indicating a string ordering of the number of areas,-n is the use of a pure number sorting (see 3rd column sort)

3.-k3,5 represents a string sort from 3rd to 5th regions,-R for reverse ordering



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



WC does not follow any options, directly with the document, it will be the number of rows, words, characters in turn output.



Uniq command
Uniq is used to remove duplicate rows, the command-C option is more commonly used, and-C indicates the number of rows that are counted and writes the number of rows in front.
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


Command Tee
Tee followed by the filename, its role is similar to redirect,, the specific gravity of a function, the file is written to the following file and displayed on the screen, usually used for pipe character |


#echo “aaaaaaaaaaaaaaa” |tee testb.txt
aaaaaaaaaaaaaaa
#cat testb.txt
aaaaaaaaaaaaaaa


Command TR
TR is used to replace characters, which are commonly used to handle special symbols appearing in documents, such as the symbol ^m that appear in a DOS document.
Options:


-D means to delete a character followed by the character to be deleted.
-S means to remove duplicate characters.


Turn lowercase letters into uppercase letters, such as TR ' [A-z] ' [A-z] ':


#head –n2 /etc/passwd |tr’[a-z]’’[A-Z]’
ROOT:X:O:O:ROOT:/ROOT:/BIN/BASH
BIN:X:11:BIN:/BIN:/SBIN/NOLOGIN


You can also replace one character:


#grep  ‘root’ /etc/passwd |tr ‘r’ ‘R’      
Root:x:o:o:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin


Command split
Split is used for cutting documents, and the common options are-B and-L.
Options:


-B means that the document is split by size in bytes
-L to divide the document by the number of rows

split -b
#mkdir split_dir
#cd !$
cd split_dir
#cp /etc/passwd ./
#split –b 500 passwd
#ls
passwd xaa xab xac xad xae


If split does not specify a target file name, it will be Xaa, Xab ... Such a filename to access the cut file. Specify the target file name, as follows:


# rm –f xa*
# split –b 500 passwd 123
# ls
# 123aa 123ab 123ac 123ad 123ae passwd


-L: Split document by number of rows


Split -l
# rm -f 123a*
# split -l 10 passwd
WC -l *
45 passwd
10 XAA
10 XAB
10 XAC
10 XAD
5 XAE
90 total consumption
Ii. some other special symbols of the shell


1. Special Symbol $
$ can be used as an identifier in front of a variable, and you can! Used in combination.


#cd ..
#ls testb.txt
testb.txt
#ls !$            
ls testb.txt
testb.txt


!$ represents the last variable of the previous command, and in this case the last command is Testb.txt, then entering!$ under the current command represents Testb.txt.



2, special symbols;
To run two or more two commands in a single line of commands, you need to add a symbol between the commands.



#mkdir Testb.txt; Touch Test1.txt; Touch Test2.txt; Ls–d test* Creating directories, creating files, listing directories



3. Special Symbols ~
The symbol ~ Represents the user's home directory, the root user's home directory is/root, the normal user's home directory is/home/username.


#cd ~
#pwd
/root
#su ccj
#cd ~
#pwd
/home/ccj


4. Special Symbols &
If you put a command in the background, you need to add the symbolic &, which is usually used for long commands. For example, it can be used after sleep, as shown below:


#sleep 30 &amp;
[1]3008
#jobs
[1] + sleep 30 in operation&amp;


5. REDIRECT Symbols >, >>, 2> and 2>>


and >> the meaning of substitution and append respectively. When we run a command error, the error message is output to the current screen. If you want to redirect to a text, use the redirect symbol 2> or 2>>, which represent error redirection and error append redirection, respectively.

#ls AAAA
Ls: can't access AAAA: no file or directory
#ls aaaa 2> /tmp/error
#cat /tmp/error
Ls: unable to access AAAA: no file or directory
#ls aaaa 2>> /tmp/error
#cat /tmp/error
Ls: unable to access AAAA: no file or directory
Ls: unable to access AAAA: no file or directory


6. Brackets []
The combination of characters in brackets, representing any of the character combinations, can also represent a range (1-3,a-z).


#cd /tmp/10
#ls –d test*
test1.txt  test2.txt  testb.txt  testdir
#ls –d test[1-3].txt
test1.txt  test2.txt
#ls –d test[12b].txt
test1.txt  test2.txt  testb.txt
#ls –d test[1-9].txt
test1.txt  test2.txt  
#ls –d test[1-9a-z].txt
test1.txt  test2.txt  testb.txt


7. Special Symbols && | |
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 "| |" are all listed below:


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.



Special symbols and Cut,sort,wc,uniq,tee,tr,split commands in the shell


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.