First day of linux Shell Study Notes

Source: Internet
Author: User

The following are Shell Study Notes for 1-2 days:

--------- I am a separator ---------

Hardware is executed, and operations are performed between the kernel and hardware.

Command parser.

Shell script Components


  1. System commands
  2. Text processing tools (sort, grep, sed, awk ...)
  3. Variable
  4. Condition judgment
  5. Cycle Structure
  6. Function

    Shell Scripts Center (SSC)

----------------------------

Non-negative Grep/awk

Several days. Wc statistics

Sort sorting sort | head/sed
Field Processing awk

Awk for data region determination | wc

Show text: echo [Option] Text

-N do not wrap automatically

-E parse escape characters

A issues a warning.

C does not end with a line break.

F line feed

R press ENTER

Color text


Echo-e "33 [40; 35 m...]"

Echo "Hello, world"


Echo-e "33 [40; 35 mHell, world33 [0 m"



Sort [Option] Input File

-O output file

-D sort a-z in alphabetical order

-N: Output 0-9 in numbers

-R: returns z-a 9-0 in reverse order.

-K indicates that the category is a digital classification in the domain.

-T-domain delimiter. Use non-spaces or tab keys to separate fields.


More/etc/passwd display by PAGE

Sort-d/etc/passwd | In The Middle Of more | indicates the opposite symbol. Hand over the processed items to the processed items and sort them first. Pagination


Sort-k3-n-r-t:/etc/passwd | more


From big to small

Wc [parameter] [file name]

-C: Count characters

-L number of statistics rows

-W: count the number of words, separate by Spaces


Diff [Option] File/directory file/directory

Diff/etc/passwd. bak

-Q: only whether there are any differences. No detailed information is displayed.

-C: display all internal text and mark the differences.

-B does not check the difference of space characters

-B does not check blank rows

-R compares the files in the subdirectory.

Grep search and matching support regular expressions


Grep

-C: only prints the number of matching rows.

-I ignore case sensitivity when matching text

-N: the row number is displayed before each row.

-V reverse output: Print unmatched rows

-F file: the list of strings to be matched is in filezhong

Grep '[Tt] his' file1.txt set Case sensitivity

Grep '^ [^ #]' file2.txt does not match the beginning of the line


Grep "s... N "file3.txt matches any character


Sed line edit command
S substitution

I insert

A Additional

D. Delete all matched rows.

D. Delete the first matched row.

What can sed do (based on "rows)

Delete

Change

Add

Insert

Replace

Example

Sed-n '1, 4p'/etc/passwd p is printed-n does not show the original file printing 1-4 rows

Sed '/80/d' file.txt deletes the first 80 rows

Sed's/var/usr/G' file.txt replace all var WITH usr g to indicate all

Sed '50, $ s/help/man/G '.

Awk can process columns or rows. It can locate the row number and column number.

Awk [Option] 'awk script' input file

-F fs uses fs as the field separator of the input record = sort-t

-F filename: Read awk_script (awk script) from file filename)

-V var = value: Set the variable for awk_script

Awk built-in Variables

Variable Function Default
FS = capital F Input field separator Space or tab
RS Input record Separator Line feed
OFS = out FS Output field separator Space or tab
ORS Output record Separator Line feed
Common NF Number of non-empty fields in the current record
Common NR Number of records read from all files

Example

Awk-F: '{print NR, $1, $2}'/etc/passwd


Awk-F: '{print NR, $1, $ NF}'/etc/passwd $ NF extract the last line

Awk-F: 'nr % 10 = 5 {print nr, $0} '/etc/passwd nr = number of currently processed rows, except 10 for the remainder = 5 to PRINT the current number of rows, output $0 in the whole line, such as 5, 15, and 25.

Awk-F: 'nr = 8, NR = 13 {print NR, $0} '/etc/passwd specifies 8 rows and 13 rows.

How awk works (tools)


$ NF $ NR $1 first column $2 second column $0 a whole row

Actual test

Awk-F: '{print $1, $3}'/etc/passwd


Awk-F: '{print $1, $ NF}'/etc/passwd


Awk-F: '{print NR, $1, $ NF}'/etc/passwd NR row number


Awk-F: 'nr = 5, NR = 8 {print NR, $1, $ NF} '/etc/passwd specify 5-8 rows


Awk-F: 'nr % 10 ==5 {print NR, $1, $ NF} '/etc/passwd except 10


Course example test

1. grep-v "-" file.txt removes all data with a negative sign.-If no value is displayed in v, only-is displayed.

2. wc-l file.txt shows all rows grep-v "-" file.txt | wc-l counts all rows without negative numbers

3. sort-n-r-k4 file.txt | sed-n '1p' the fourth column of sorting has no separator (that is, space ), -n number-r from large to small sed the pipe shows the first line sed-n does not show the original file

Sort-n-r-k4 file.txt | head-1 reads the first row of the first data head

4. awk '{if ($ NF> 3) {print $0}'. if the last column of file.txt NF is greater than 3, a whole row is printed.

5. awk '{if ($4> 0, & $4 <15) {print $0}' file.txt lists the entire output row with the fourth column greater than 0 and less than 15

Awk '{if ($4> 0, & $4 <15) {print $0}' file.txt | number of lines displayed by wc-l

Exercise:


  1. Grep and awk

    Awk '{if ($1> 200801011) {[print snapshot 01_folder 'file.txt

2,

Processing Command review:

Statistical text wc

Text sorting sort

Text/directory comparison diff

Find the key line grep/sed in the file

Add, delete, and modify sed in row text

Display the specified column awk in the column text

Calculate awk in column text

Select awk in column text

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.