7. Text processing, redirection, pipelines, bash arithmetic operations, script input

Source: Internet
Author: User

Text processing commands:

    Wc, tr, cut, sort, uniq, all of them are simple, but they are very common,


1. wc: this is a command for character statistics,

Wc [option] [file]...

-L: number of statistics rows

-C: counts the number of bytes.

-W: count the number of words (the words here refer to strings separated by spaces)

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKiom1RZ-w2BShVmAABd2silW9U327.jpg "title =" wc.png "alt =" wKiom1RZ-w2BShVmAABd2silW9U327.jpg "/>


2. tr: conversion character or delete character. This command is mainly used for case-insensitive conversion, replacement or deletion of string

-D: deletes character strings.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CB/wKioL1RZ_HuQ6TDYAAB_6ygBJoY220.jpg "title =" tr.png "alt =" wKioL1RZ_HuQ6TDYAAB_6ygBJoY220.jpg "/>


3. cut: cut text. The text here is a single line, that is, cut can only process one line of text at a time.

-D character: specifies the delimiter

-F #: specify the field to be displayed

Single number: displays a field

Multiple numbers separated by commas (,): multiple fields are displayed.

-: Continuous fields, such as 3-5

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/CB/wKioL1RZ_jywSIcpAAEKyKqOVc4113.jpg "title =" cut aaaaa5.png "alt =" wKioL1RZ_jywSIcpAAEKyKqOVc4113.jpg "/>


4. sort: Sorting. By default, it is sorted by characters, that is, ASCII code values. However, you can also specify numerical sorting.

Sort [option] file...

-F: case-insensitive characters

-N: compare the value.

-T: specifies the delimiter.

-K: specifies the fields to be compared after separation.

-U: Duplicate rows, only once

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKiom1RaAHLTyrKQAAByHJ6nIhY921.jpg "title =" sort.png "alt =" wKiom1RaAHLTyrKQAAByHJ6nIhY921.jpg "/>

By default, sort processes files in the unit of action ..


5. uniq

-C: displays the number of repeated rows.

-D: only duplicate rows are displayed.

-U: displays rows that have never been repeated.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CB/wKioL1RaAwngM67mAACljzV-pOw335.jpg "title =" uniq.png "alt =" wKioL1RaAwngM67mAACljzV-pOw335.jpg "/>

OK. Here are two exercises,

Exercise 1: count the number of files in/bin,/usr/bin,/sbin,/usr/sbin, and other directories;

# Ls/bin | wc-l

Exercise 2: 2. Display the shells of all users on the current system. It is required that each shell be displayed only once;

# Cut-d:-f7/etc/passwd | sort-u

Redirection:

    By default, the system displays the output information on the monitor. However, sometimes we want to store the displayed information directly as a file, which requires redirection ..

Input and output are actually divided into three types, which are displayed on the display by default ..

Standard input: stdin: 0

Standard output: stdout: 1

Standard error: stderr: 2


I/O redirection:

Input redirection:

<: Command <FILE: This indicates that the command obtains stdin in the form of a file (previously input by keyboard ...)

<: Command <flag string: this is called Hear document. It is read from stdin until 650 is stopped when a flag string is encountered.) this. length = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKioL1RaCPnzfRHbAACuz35MGQ0437.jpg "title =" stdin.png "width =" 700 "height =" 192 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 700px; height: 192px; "alt =" wkiol1racpnzfr?acuz35mgq0437.jpg "/>

Input redirection:

>: Command> File: save the command output to the file (the original content of the file will be cleared)

>>: Command> File: append the command output to the file, which will not empty the original file ..

Set-C: Do not use the> command, because this command is dangerous,

Set + C: allow to use> Command

>|: This can be used when the command is disabled, which indicates mandatory usage.

/Dev/null: this is a file, also called a bucket. No matter what is redirected to it, 650 will be cleared.) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CD/wKiom1RaC9Lyy-GxAAClJ5sRCT4399.jpg "title =" sssssww.png "alt =" wKiom1RaC9Lyy-GxAAClJ5sRCT4399.jpg "/>

Here, the output result of the id command is redirected to a bucket (it feels like a garbage bin ..), Then let's see if the command is successfully executed. If it succeeds, it indicates that this user exists, which seems to be used in this way.


Error redirection: This is similar to the input redirection,

2>: OVERWRITE

2>: append

       

Here, the successful execution of a command is not the same as the execution error. Therefore, if output redirection is used, the file will still display an error message when the file fails to be executed .. 650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CC/wKioL1RaDV2QPi2wAABE80vrgKs242.jpg "title =" ssssswww.png "alt =" wKioL1RaDV2QPi2wAABE80vrgKs242.jpg "/>

Output and error redirection: this is the combination of output redirection and error redirection ..

&>: OVERWRITE

& >>: Append

MPs queue:

This has been said before. | indicates that the execution result of the previous command is used as the input of the next command ..


Tee: outputs the standard input content to the screen and stores it in a file. It is equivalent to a three-way interface ..

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/CD/wKiom1RaDvKAImupAAB1ndL8O4U753.jpg "title =" tee7.png "alt =" wKiom1RaDvKAImupAAB1ndL8O4U753.jpg "/>

This command also seems to overwrite the file.

Arithmetic operations in bash:


Declare: defines the type of a variable when defining a variable.

-I: integer

-X: environment variable

Let var_name = arithmetic expression. You can use the let command to perform arithmetic operations ..

Var_name = $ [arithmetic expression], the second method of arithmetic operation ..

(Var_name = arithmetic expression), the third method of the arithmetic expression..., Pitfall! Is there no unique method?

Var_name = 'expr $ num1 + $ num2 ', which is the fourth type. bash is always messy.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKioL1RaE6KhVHW1AAEsdmsUwhg299.jpg "title =" arithmetic .png "alt =" wKioL1RaE6KhVHW1AAEsdmsUwhg299.jpg "/>


Read and single-step execution:


Read: indicates the input ..

Read I indicates reading the I variable. It needs to interact with the user and wait for user input.

-T: The time when the user enters the information.

-P "string": displays a string when it is read.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/D3/wKiom1Ra4I7ARolEAABps_292T4071.jpg "style =" float: none; "title =" read1.png "alt =" wKiom1Ra4I7ARolEAABps_292T4071.jpg "/>

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/D2/wKioL1Ra4POT03g7AACTbv4u3j0184.jpg "style =" float: none; "title =" read2.png "alt =" wkiol1ra4pot0%7aactbv4u3j0184.jpg "/>


One-Step execution: bash-x script name

Exercise: enter two integers A and B, where A <B calculates the sum of all integers between A and B.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/D3/wKioL1Ra46nTXoqpAABtpClfocc303.jpg "title =" script 2121.png "alt =" wKioL1Ra46nTXoqpAABtpClfocc303.jpg "/>

Now let's take a look at the process.

650) this. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/D3/wKioL1Ra4_yRFqziAADb9CtGso4621.jpg "title =" -x.png "alt =" wKioL1Ra4_yRFqziAADb9CtGso4621.jpg "/>

This article from "quiet indifferent" blog, please be sure to keep this source http://vrgfff.blog.51cto.com/6682480/1572815

7. Text processing, redirection, pipelines, bash arithmetic operations, script input

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.