2018-1-12 Linux Learning Notes

Source: Internet
Author: User

8.10 Shell Special Symbol Cut command

8.10.1 Special Symbols
Symbol *: any of any characters
Symbol? : any one character
Symbol #: Comment character, which is the content after # Linux ignores
Symbol \: Escape character, which will be followed by a special symbol (for example "") reverts to normal characters
Symbols | : A pipe character that drops the result of a preceding command to a command following a symbol, usually a command for a document operation, such as Cat,less,head,tail,grep.

8.10.2
Cut command, used to intercept a field
Syntax: cut-d "separator character" [-CF] n here n is the number
-D: Specify delimited characters
-F: Specifies the segment number (between segments and segments separated by delimiters)
-C: Specify the number of characters
<------------------------------------------------------------->
[Email protected] ~]# CAT/ETC/PASSWD | Head-n2
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
[Email protected] ~]# CAT/ETC/PASSWD | head-n2 | Cut-d ":"-f1
Root
Bin
[Email protected] ~]# CAT/ETC/PASSWD | head-n2 | Cut-d ":"-f3-5
0:0:root
1:1:bin
[Email protected] ~]# CAT/ETC/PASSWD | head-n2 | Cut-c1
R
B
[Email protected] ~]# CAT/ETC/PASSWD | head-n2 | Cut-c1-3
Roo
Bin
<------------------------------------------------------------->
Note:-C and-F can not be used at the same time, or will error, such as:
<---------------------------------------------------------------->
[Email protected] ~]# CAT/ETC/PASSWD | head-n2 | Cut-c1-3-D ":"-f1
Cut: Only one type in the list can be specified
Try ' Cut--help ' for more information.
[Email protected] ~]#
<---------------------------------------------------------------->

8.11 Sort_wc_uniq Command

The

8.11.1
Sort command, which is used for sorting.
Syntax: sort [-t delimiter] [-KN1,N2] [-NRU] Here's N1 < N2
-T: followed by delimiter, action with cut's-D one meaning
-N: Sort with a pure number (note: Like letters, special symbols such as non-numeric, the system will think it is 0) -r: Reverse Sort
-u: Go to repeat
-kn1,n2: Sort by n1 interval to N2 interval, you can write-kn1 only, sort N1 field
If sort does not have any options, then backward from the first character, followed by ASCII value, Finally, they are output in ascending order. The
8.11.2
WC Command, which is used to count the number of lines, words, and characters of a document.
-L: Count Rows
-W: Statistics number of words
-m: Statistical characters
WC does not follow any options, directly with the document, it will output the number of rows, words, characters in turn.
<-----------------------------------;
[[email protected] ~]# wc-l/etc/passwd
25/etc/passwd
[[email protected] ~]# wc-w/etc/passwd
45/etc/passwd
[[email protected] ~]# wc-m/etc/passwd
1169/ETC/PASSWD
[[email protected] ~]# wc/etc/passwd
1169/etc/passwd
[[email  Protected] ~]#

Note-M counts the number of characters, it will also count the line break!
[email protected] ~]# cat 2.txt
1234
[Email protected] ~]# wc-m 2.txt
5 2.txt
[Email protected] ~]# cat-a 2.txt
1234$
[Email protected] ~]#
<----------------------------------->
8.11.3
The Uniq command, which is used to remove duplicate rows.
-C: Count the number of repeated rows and write the number of lines in front.
Note: If you use Uniq, you need to sort the files first, otherwise it doesn't work.
<------------------------------------------->
[Email protected] ~]# Uniq 2.txt
111
333
222
111
555
[Email protected] ~]# Sort-n 2.txt | Uniq
111
222
333
555
[Email protected] ~]# Sort-n 2.txt | Uniq-c
2 111
1 222
1 333
1 555
[Email protected] ~]#
<------------------------------------------->
!! Note that the use of the Sort,uniq command does not change the contents of the original file, they only do some processing of the original file content and then output to the screen, the original file content has not changed.

8.12 tee_tr_split Command

8.12.1
Tee command
followed by the file name, similar to redirection, but redirects are also displayed on the screen, and tee is often used in the pipe after "|".
<---------------------------------------------------------->
[Email protected] ~]# Sort-n 2.txt | uniq-c | Tee 1.txt
2 111
1 222
1 333
1 555
[email protected] ~]# cat 1.txt
2 111
1 222
1 333
1 555
Tee-a similar to the additional redirect >&gt, as in the following example:
[email protected] ~]# cat 1.txt
2 111
1 222
1 333
1 555
[Email protected] ~]# Sort-n 2.txt | uniq-c | Tee-a 1.txt
2 111
1 222
1 333
1 555
[email protected] ~]# cat 1.txt
2 111
1 222
1 333
1 555
2 111
1 222
1 333
1 555
[Email protected] ~]#
<---------------------------------------------------------->
8.12.2
TR command
Used to replace characters that are commonly used to handle special symbols that appear in a document.
The most common is to capitalize the lowercase: tr ' [A-z] ' [A-z] '
<----------------------------------------------------------------->
[Email protected] ~]# echo "HelloWorld" | Tr ' h ' h '
Helloworld
[Email protected] ~]# echo "HelloWorld" | TR ' [HW] ' [HW] '
HelloWorld
[Email protected] ~]# echo "HELLOWORLDW" | TR ' [HW] ' [HW] '
Helloworldw
[Email protected] ~]# echo "HELLOWORLDW" | TR ' [A-z] ' [A-z] '
Helloworldw
<----------------------------------------------------------------->
deficiencies:
TR command substitution, deletion, and de-duplication are all for a single character, not for a string, there is some limitation.

8.12.3
Split command
For cutting documents, common options:
-B: Splits the document by size, the default unit is byte
-L: Split document by number of rows
<----------------------------------------------------------------------------------->
[[email protected] test2]# ls
1.txt
[Email protected] test2]# Du-sh 1.txt
256K 1.txt
[Email protected] test2]# Split-b 10000 1.txt
[[email protected] test2]# ls
1.txt xab xad xaf xah xaj xal xan xap xar xat Xav xax Xaz
Xaa xac xae xag xai xak xam xao xaq xas xau Xaw Xay
[Email protected] test2]# rm-f x
[[email protected] test2]# ls
1.txt
[Email protected] test2]# split-l 1.txt
[[email protected] test2]# ls
1.txt xaa xab xac xad xae xaf xag
[Email protected] test2]# Du-sh

256K 1.txt
44K XAA
44K Xab
40K Xac
40K Xad
36K xae
40K Xaf
20K XAG
[Email protected] test2]#
<----------------------------------------------------------------------------------->

8.13 Shell Special Symbol
    The
    • symbol $
      variable prefix,!$ combination, represents the last variable in the previous hit, assuming that the top command is Test.txt then the!$ in the current command represents Test.txt. In the regular, it means the end of the line.
    • symbol;
      When multiple commands are written to one line, they are delimited by semicolons.
    • Symbol ~
      User's home directory, if Root is/root, normal user is/home/username
    • symbol & The
      is placed behind the command, and the command is dropped to the background for execution.
    • symbol;
      Redirect
    • symbol >>
      Chase emphasis orientation
    • symbol 2>
      Error redirection
    • symbol 2>>
      Error Append redirect
    • symbol &>
      Redirects the
    • symbol []
      One of the specified strings, regardless of the result of the command execution (correct/error), [0-9],[A-ZA-Z],[ABC]
    • symbol & &
      is used between commands, such as Command1 && Command2, and Command2 does not execute until Command1 execution succeeds, otherwise command2 does not.
      Symbol | |
      used between commands, such as Command1 | | Command2,command1 after successful execution Command2 not execute, otherwise go to execute Command2, in short Command1 and Command2 always have a command to execute.

Example of a demo for &> :
<----------------------------------------------------------------->
[[email protected] ~]# ls
2.txt 3.txt anaconda-ks.cfg err.txt result.txt test test2
[email protected] ~]# cat Result.txt
[email protected] ~]# cat 2.txt
111
333
222
111
555
[[email protected] ~]# cat 2.txt &> result.txt
[email protected] ~]# cat Result.txt
111
333
222
111
555
[[email protected] ~]# >result.txt # empty Result.txt
[email protected] ~]# cat Result.txt
[[email protected] ~]# ls 1.txt &> result.txt
[email protected] ~]# cat Result.txt
LS: unreachable 1. txt: no file or directory
[Email protected] ~]#
<----------------------------------------------------------------->

2018-1-12 Linux Learning Notes

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.