Linux Shell Programming-character intercept commands

Source: Internet
Author: User
Tags clear screen

01. Field Extraction Command Cut + = cut [option] file name

-F Column Number: Extract the first few columns

-D delimiter: Splitting columns by the specified separator

For example: Cut-f 2,4 (column number;, comma can extract multiple rows) student.txt

Cut-d ":"-F 1,3/ETC/PASSWD

02. Formatted OUTPUT command printf command = printf ' output type output format ' output

Output Type:

%ns: Output string. N is a numeric reference to output several characters

%ni: Output integer. N is a digital reference to output several numbers

%M.NF: Output floating-point number. M is an integer, n is a decimal

Output format:

\a: Output Warning sound

\b: Output backspace key, delete key

\f: Clear Screen

\ n: Line break

\ r: Enter

\ t: Horizontal output BACKSPACE key

\v: Vertical Output BACKSPACE key

03. awk Command = + awk ' condition 1{action 1} Condition 2{action 2} .... ' File name condition (Pattern)

Actions (Action):

Formatted output

Process Control Statements

For example: awk ' {printf $ "\ t" $6 "\ n"} ' Student.txt

$6: On behalf of the first few lines

BEGIN = awk ' begin{print "Test!"} {printf $ "\ t" $6 "\ n"} ' Student.txt

BEGIN executes before all data is processed

Example: FS built-in variables

cat/etc/passwd | grep "/bin/bash" | \ awk ' BEGIN {fs= ': '} {printf $ "\ t" $ "\ n"} '

End = opposite: All data is executed after processing

04. Sed command: Is a lightweight flow editor that is almost included on all UNIX platforms, including Linux.

SED is primarily used to select, replace, delete, and add commands to the data.

sed [options]-' [action] '-file name

-N: The general sed command will output all data to the screen, and if you add this option, only the lines processed by the SED command will be output to the screen

-e: Allow multiple sed command edits to input data

-I: Directly modifies the file that reads the data with the result of the SED modification, rather than the screen output

Action:

      

Print, output specified line: sed-n ' 2p ' student.txt

Delete: sed "2,4d" student.txt = delete 2--4 rows of data, but do not modify the file itself

Append: sed ' 2a hello ' student.txt append hello after second line

Insert: sed ' 2i wrold Hello ' student.txt insert two rows of data before the second line

String substitution: Sed-i ' 2s/99/55/g ' Student.txt replaces 99 of line 2nd with 55

Plus-I: modified with the file, not only for printing changes

Add-e:sed-e ' s/liming/ads/g;s/gao/ads/g ' student.txt

Colleagues replace "liming" and "Gao" with ads

05. Sort Command = sort [option] file name

Options:

-F: Ignore case

-N: Sort by numeric type, by default using string type sorting

-R: Reverse Sort

-T: Specify delimiter, default is tab

-K n[,m]: Sorts by the specified field range. Starting with the nth field, the M field ends (default to end of line)

Example: Sort-t ":"-K 3,3/etc/passwd (not normally required)

Specifies that the delimiter is ":", starting with the third field, sorting at the end of the third field, sorting only the third field (User uid sort)

06. Statistics command = WC [option] File name

Options:

-L: Count rows only

-W: Counts only the number of words

-M: Count only characters

      

Linux Shell Programming-character intercept 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.