10th Shell Programming (2) _ Character intercept command

Source: Internet
Author: User
Tags clear screen disk usage

2. character intercept commands

2.1 Cut field Fetch command (grep fetch row, cut fetch column )

(1) Cut command:#cut [options] File name

Option:-F Column Number: Extract the first few columns;

-D delimiter: Splits columns by the specified delimiter, which is tab delimited by default .

(2) Application examples

Test file (student.txt)

idnamegenderphplinuxmysqlaverage1zsm82958687.662lsf74968785.663wwm99839391.66

① #cut –f 2 student.txt//Extract 2nd Column

② #cut –f 2,3 student.txt//extract 2nd, 3 columns

③ #cut –d ":" –f 1,3/etc/password//with ":" to separate columns, extracting 1th, 3 columns.

④ Bulk deletion of ordinary users:

# CAT/ETC/PASSWD | Grep/bin/bash | Grep-v Root | Cut-d ":"-F 1 ( Note that the /bin/bash script is executed only if root and normal user are logged on.) Grep–v root to remove the root user, then only the ordinary user, and then the cut to extract the user name)

(3) Cut command limitations : If separated by a space, you will encounter problems in calculating the space inconvenience (for example, #df –h display information is separated by a space, can be resolved using the awk command)

2.2 printf Command

(1) formatted output command:printf ' output type output format ' output content

Output type

Description

%ns

The output string. N is a numeric reference to output several characters

%ni

The output integer. N is a digital reference to output several numbers

%m.nf

Output floating-point numbers. M and n are numbers that refer to the integer and decimal digits of the output. such as%8.2f represents a total output of 8 digits, of which 2 decimal places, 6 bits are integers.

Output format

Description

\a

Output Warning Sound

\b

Output backspace key, i.e. backspace key

\f

Clear Screen

\ n

Line break

\ r

Carriage return, which is the ENTER key

\ t

Horizontal Tab key

\v

Vertical Tab key

(2) Application examples

① #printf '%s%s '%s ' 1 2 3 4 5 6///divided into two groups "1 2 3", "4 5 6", and then output

② #printf '%s%s\n ' 1 2 3 4 5 6//divided into two groups "1 2 3", "4 5 6", and wrap output

③ #printf '%s ' $ (cat student.txt)//Use the Cat system command to query the contents of the Student.txt and display it with printf.

④ #printf '%s\t%s\t%s\t%s\t%s\t%s\t\n ' $ (cat student.txt)//formatted output

(3) Support for print and printf commands in the output of the awk command

  ①print: A newline character is automatically added after each output (Linux default does not have a print command, but the awk command comes with a print command)

  ②printf: is the standard format Output command, and does not automatically add line breaks , if you need to wrap, you need to add a newline character manually.

2.3 awk Command

(1) Introduction to the awk command

awk is an acronym for three people: Aho, (Peter) Weinberg and (Brain) Kernighan. It was these three people who created awk---an excellent scanning and processing tool for style.

What is the function of ②awk? Similar to SED and grep, awk is a style scanning and processing tool. But its functions are much stronger than sed and grep. Awk provides extremely powerful functionality: it can do almost everything grep and sed can do, and it can also be used for style loading, flow control, mathematical operators, Process control statements, and even built-in variables and functions . . It has almost all the beautiful features that a complete language should have. In fact, Awk does have its own language: The AWK programming language, and Awk's three-bit creator has formally defined it as: style scanning and processing language.

(2) command format:#awk ' condition 1{action 1} Condition 2{action 2} ... ' File name

Condition (Pattern)

Description

Actions (Action)

X>10

Determine if the variable x is greater than 10

Formatted output

x>=10

Greater than or equal to 10

Process Control Statements

▲ general use of relationship expressions as conditions

The "Programming Experiment" awk command

① #awk ' {printf $ "\ t" $7 "\ n"} ' student.txt//Display Student.txt 2nd, 7 columns and format output with TAB key

Name Average

ZS 87.66

LS 85.66

WW 91.66

②# Df-h | awk ' {print $ \ t ' $ "\ $6} '//display disk information

Filesystem use% Mounted

/dev/sda5 8%/

Tmpfs 0%/dev/shm

/DEV/SDA1 16%/boot

/dev/sda2 1%/Home

/DEV/SDB1 1%/disk1

/DEV/SDB5 1%/disk5

③ Extracting disk usage

(3) BEGIN: Execute the statement after begin before all commands are executed

① display the prompt message before displaying the command result

# awk ' begin{printf ' This is a transcript\n "}{printf" \ T "$6" \ n "} ' Student.txt

This is a transcript

Name MySQL

ZS 86

LS 87

WW 93

② Extract the 1th and 3rd fields of/etc/passwd with ":" as delimiters

#awk ' begin{fs= ': "}{print" \ T "$/etc/passwd} '

(4) END: After all commands have been executed, execute the statement after the end

①# awk ' end{printf ' the end\n "}{printf" \ T "$7" \ n "} ' Student.txt

(5) fs built-in variables: Separators

(6) relational operator:# cat Student.txt | Grep-v Name | awk ' $7 >=87{printf "\ n"} '

2.4 sed Command

(1) Introduction to SED commands

SED 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.

(2) SED command:#sed [Options] ' [action] ' file name

Options

Description

-N

The general SED command will output all data to the screen, and if you join this, only the lines processed by the SED command will be output to the screen.

-E

Allows multiple sed command edits to be applied to input data

-I.

Directly modifies the file that reads the data with the result of the SED modification, rather than the output from the screen. That is, the modified result is written to the file.

Action

Description

A

Append to add one or more rows after the current line. When multiple rows are added, the "\" at the end of each line is required to indicate that the data is not finished except for the last row.

C

Row substitution, replacing the original data row with a string after C, replacing multiple rows, with the "\" on the end of each line in addition to the last line, which means the data is not completed

I

Insert, inserting one or more rows before the current line. When multiple rows are inserted, the "\" at the end of each line is required to indicate that the data is not finished except for the last row.

D

Delete, delete the specified line

P

Print, output the specified line

S

String substitution, replacing another string with one. The format is "line range s/old string/New strings/g" (similar to the replacement format in vim)

(3) Row data manipulation

①# sed-n ' 2p ' student.txt//View the 2nd line of the file. Note that if you do not add-N, the original file will be output again, in addition to the 2nd line

②# sed ' 2,4d ' student.txt//delete rows 2nd through 4th without affecting the file itself

③# sed ' 2a hello ' student.txt//Add Hello after line 2nd

④# sed ' 2i Hello World ' student.txt//Insert Hello World before line 2nd

⑤# sed ' 2i hello \//Insert two lines before line 2nd: Hello and world

World ' Student.txt

⑥# sed ' 2c No such person ' student.txt//replaces the 2nd row of data with the specified text.

(4) string substitution: #sed ' s/old string/new string/g ' file name

①# sed ' 3s/74/99/g ' student.txt//In line 3rd, change 74 to 99

②# sed-i ' 3s/74/99/g ' student.txt//sed operation data is written directly to the file and is not displayed on the screen.

③# sed-e ' s/zs//g;s/ls//g ' student.txt//Replace "ZS" and "LS" with empty

10th Shell Programming (2) _ Character intercept command

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.