Linux Shell Learning Notes first day _ other

Source: Internet
Author: User
Tags diff numeric first row

The following is a 1-2-day study of Shell learning notes

————————-I'm the separator —————————

Hardware to perform, operating between the kernel and the hardware.

The command parser.

Component elements of a shell script


    1. System commands
    2. Text Processing tools (sort, grep, sed, awk ...) )
    3. Variable
    4. Conditional judgment
    5. Circular structure
    6. Function

      Shell Scripts Center (SSC)

———————————————————————————-

Non-negative Grep/awk

Few days. Statistical WC

Sort sorting sort|head/sed
Field Handles awk

Data Area Judge Awk |WC

Display text: echo [options] Text

-N Do not wrap lines

-E Resolution escape character

A issued a warning tone

c Finally, no line-wrapping symbol

F Line Wrapping

R carriage

Color text


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

echo "Hello,world"


Echo–e "33[40;35mhell,world33[0m"



sort [Options] input file

-O Output file

-D Sort A-Z by dictionary order

-N Output 0-9 by numeric size

-R Reverse Output z-a 9-0

-k Specifies that the category is a numeric category on a field

-t field separator, split field with non-space or TAB key


MORE/ETC/PASSWD pagination Display

In the middle of the Sort–d/etc/passwd|more | is the symbol. Sort the previous processing to the back processing first. Post-page display


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


From big to small

WC [parameters] [filename]

-C statistics Number of characters

-L Statistic Line count

-W Statistics word number space distinction


diff [Options] File/directory File/directory

Diff/etc/passwd/etc/passwd.bak

-Q only shows no differences, no detailed information is displayed

-C Displays all the text and marks the difference.

-B does not check for differences in space characters

-B Do not check blank lines

-R to compare files in subdirectories.

grep search matching supports regular expressions


Grep

-C Print only the line number count of the matching pattern

-I ignores case when matching text

-N displays its row number before each line

-V reverse output, print mismatched rows

-F file to match the list of strings in Filezhong

grep ' [tt]his ' file1.txt set case

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 attach

d Delete all matching rows

D Delete first-match rows

What SED can do ("line" based)

Delete

Change

Add to

Insert

Replace

Example

Sed–n ' 1,4p '/etc/passwd p is print-n does not display the original file print 1-4 lines

Sed '/80/d ' file.txt first occurrence of 80 rows to delete

Sed ' s/var/usr/g ' file.txt replaces all var with usr G to represent all

Sed ' $s/help/man/g ' file.txt replaces help from 50 lines to the last line to man before 49 no matter

Awk can work with columns, or you can work with rows, you can navigate to the first few lines, the first columns

awk [Options] ' awk script ' input file

-F FS uses FS as the field delimiter for input records = Sort–t

-f filename reads awk_script from file filename (awk script)

-V var=value set variable for Awk_script

Built-in variables for awk

/tr>
variable feature default /td>
FS = capital F Enter field delimiter space or tab /td>
, Enter record delimiter newline
OFS =out FS output field delimiter space or tab
ORS output Record separator break line
NF common current record non-null field number  
NR common record number read from all Files  

Example

Awk–f: ' {print nr,$1,$2} '/etc/passwd


Awk–f: ' {print nr,$1, $NF} '/etc/passwd $NF fetch last row

Awk–f: ' Nr%10==5{print nr,$0} '/etc/passwd nr= the number of rows currently processed is in addition to 10 = 5 Printing The current number of rows, outputting the entire line of output, such as 5, 15, 25 print out

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

How awk works (Tools)


$NF $NR $ A row in the first column and the second column of $

Actual test

Awk–f: ' {print $1,$3} '/etc/passwd


Awk–f: ' {print $, $NF} '/etc/passwd


Awk–f: ' {print nr,$1, $NF} '/etc/passwd NR line number


Awk–f: ' Nr==5,nr==8{print nr,$1, $NF} '/etc/passwd Specify 5-8 lines


Awk–f: ' Nr%10==5{print nr,$1, $NF} '/etc/passwd except 10


Course Sample Test

1, Grep–v "-" file.txt remove all the data with minus sign-V to show that none of the words show only-

2, wc-l file.txt display all Rows grep–v "-" file.txt |wc–l statistics all without negative numbers

3, sort–n–r–k4 file.txt |sed–n ' 1p ' sort fourth column without delimiters (that is, spaces),-n digits –r from large to small sed pipe shows the first row sed–n does not display the original file

SORT–N–R–K4 file.txt |head-1 Read First Data head first line

4. awk ' {if ($NF >3) {print $}} ' file.txt NF the last column is greater than 3 print an entire line

5. awk ' {if ($4>0,&&$4<15) {print $}} ' file.txt list fourth column greater than 0 less than 15 output whole line

awk ' {if ($4>0,&&$4<15) {print $}} ' file.txt |wc–l display number of rows

Practice:


    1. grep and awk

      awk ' {if ($1>200801011) {[Print $}} ' file.txt

2,

Processing Command review:

Statistic text WC

Sorting text sort

Text/Directory comparison diff

Find key rows in a file grep/sed

Add, delete, and change sed in row text

Display the specified column in the column text awk

Calculate in column text awk

Make conditional selections in column text awk

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.