22.Shell Special symbols and Cut,sort,wc,uniq,tee,tr,split commands

Source: Internet
Author: User

Five Week fifth session (January 12)

8.10 Shell Special Symbol Cut command

8.11 Sort_wc_uniq Command

8.12 tee_tr_split Command

8.13 Shell Special Symbol

Related Quiz title: http://ask.apelearn.com/question/5437

Extended

    1. SOURCE Exec Difference http://alsww.blog.51cto.com/2001924/1113112
    2. Linux Special symbols Daquan http://ask.apelearn.com/question/7720
    3. Sort is not sorted by ASCII http://blog.csdn.net/zenghui08/article/details/7938975
A special symbol
    • [] * Any arbitrary character, wildcard characters

    • [ ] ? Any one character

    • [] # comment characters

    • [] \ de-semantic characters

    • [ ] | Pipe character
[email protected]:~# a=1[email protected]:~# b=2[email protected]:~# c=$a$b[email protected]:~# echo $c12[email protected]:~# c=\$a\$b[email protected]:~# echo $c$a$b
Two commands related to pipelines
    • [] Cut split,-D delimiter-F Specify segment number-C to specify the first word
[email protected]:~# cat /etc/passwd |head -2root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin[email protected]:~# cat /etc/passwd |head -2 |cut -d ":" -f 1 //只取第一段rootbin[email protected]:~# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 //取第一段,第二段root:xbin:x[email protected]:~# cat /etc/passwd |head -2 |cut -c 4t:
    • [] Sort sort,-n with numeric sort-R reversed-T delimiter-kn1/-kn1,n2
[email protected]:~# sort -r /etc/passwdxavi:x:1000:1000:xavi,xavi‘s office,62580558,62589906:/home/xavi:/bin/bashxavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bashusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinunbound:x:993:991:Unbound DNS resolver:/etc/unbound:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologintest:x:1002:1002::/home/test:/bin/bash
    • [] wc-l statistics number of rows-m statistic characters-W statistics
[email protected]:~# vi 2.txt[email protected]:~# cat 2.txt2221dedddededed121332e4ded[email protected]:~# wc -l 2.txt6 2.txt[email protected]:~# wc -m 2.txt32 2.txt[email protected]:~# wc -w 2.txt6 2.txt
    • [] Uniq, C count the number of rows, the contents of the file is unchanged

[email protected]:~# sort 2.txt|uniq  //必须先给文件排序,才能生效111121322221dedd3323232e4444deddede
[email protected]:~# sort 2.txt|uniq -c  //统计重复次数,      1 1      1 11      1 1213      2 2      1 2221dedd      2 3      1 3232      1 32e4      1 444      2 ded      1 dede
    • [] tee and > Similar, redirection is also displayed on the screen
[email protected]:~# echo "dadsaddsdad" |tee a.txtdadsaddsdad[email protected]:~# sort 2.txt|uniq -c |tee a.txt      1 1      1 11      1 1213      2 2      1 2221dedd      2 3      1 3232      1 32e4      1 444      2 ded      1 dede
    • [] te-a and >> similar, chasing aggravated orientation

      [email protected]:~# sort 2.txt|uniq -c |tee -a a.txt  1 1  1 11  1 1213  2 2  1 2221dedd  2 3  1 3232  1 32e4  1 444  2 ded  1 dede[email protected]:~# cat a.txt  1 1  1 11  1 1213  2 2  1 2221dedd  2 3  1 3232  1 32e4  1 444  2 ded  1 dede  1 1  1 11  1 1213  2 2  1 2221dedd  2 3  1 3232  1 32e4  1 444  2 ded  1 dede
    • [] TR replacement character, tr ' a ' B ', case replacement TR ' [A-z] ' [A-z] '//[] means any number of characters in parentheses are selected
[[email protected] ~]# echo "xavilinux" |tr ‘[al]‘ ‘[AL]‘xAviLinux[[email protected] ~]# echo "xavilinux" |tr ‘a‘ ‘A‘xAvilinux
    • [] Split cut,-B size (default unit byte),-L line number

-B means that the document is split 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 -b 500 passwd[[email protected] split_dir]# lspasswd  xaa  xab  xac  xad  xae

To see the actual size, Du-sh is looking at the block, du-sb the size by byte

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:

[[email protected] split_dir]# rm -f x* //把前面分割好的文件全部删除[[email protected] split_dir]# split -b 100 passwd 123[[email protected] split_dir]# ls123aa  123ad  123ag  123aj  123am  123ap  123as  123av  passwd123ab  123ae  123ah  123ak  123an  123aq  123at  123aw123ac  123af  123ai  123al  123ao  123ar  123au  123ax

-L to divide the document by the number of rows

[[email protected] split_dir]# rm -f 123*[[email protected] split_dir]# split -l 10 passwd //10行分割[[email protected] split_dir]# wc -l *  46 passwd  10 xaa  10 xab  10 xac  10 xad   6 xae  92 总用量
Special symbols for three shells 1, special symbols $

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

[[email protected] /]# touch 1.txt[[email protected] /]# ls 1.txt1.txt[[email protected] /]# ls !$ls 1.txt1.txt

!$ represents the last variable of the previous command, in this case the last command is 1.txt, then entering!$ under the current command means 1.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.

[[email protected] /]# ls 1.txt ; wc -l 2.txt1.txt8 2.txt[[email protected] /]# ls 1.txt;wc -l 2.txt //命令之间不空格也是有效的,1.txt8 2.txt
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.

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:

[[email protected] /]# sleep 30 &[2] 7047[[email protected] /]# jobs[1]+  已停止               wc -l(工作目录:~/split_dir)[2]-  运行中               sleep 30 &
5. REDIRECT Symbols >, >>, 2> and 2>&gt, error and correct input &>

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.

6. Brackets []

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

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.

[[email protected] /]# ls 1a.txt; wc -l 2.txtls: 无法访问1a.txt: 没有那个文件或目录[2]-  完成                  sleep 308 2.txt[[email protected] /]# ls 1.txt || wc -l 2.txt1.txt[[email protected] /]# ls 1.txt && wc -l 2.txt1.txt8 2.txt[[email protected] /]# ls 1a.txt && wc -l 2.txtls: 无法访问1a.txt: 没有那个文件或目录

22.Shell Special symbols and Cut,sort,wc,uniq,tee,tr,split commands

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.