Linux Sed,awk detailed

Source: Internet
Author: User
Tags echo command uuid

SED command:
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.
SED usage parameters
[[Email protected] ~]# sed [-NEFR] [action]
-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the terminal.
However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Action editing of SED directly in command-line mode;
-F: The action of SED is written directly in a file, and-f filename can run the SED action within filename;
-r:sed's actions support the syntax of extended formal notation. (The default is the basic formal French notation)
-I: Directly modifies the contents of the read file, not the output to the terminal.
s/regexp/replacement/: Replace the content that was matched by regexp to replacement
W/path/to/somefile: Save the specified content to the file specified by the/path/to/somefile path
R/path/from/somefile: Inserts all the contents of another file at the specified location in the file, completing the file merge
function
A: New, a can be followed by a string, and these strings will appear on a new line (the next line)
C: Replace, C can be followed by a string, these strings can replace the line between N1,N2
D: Delete, because it is deleted ah, so d usually do not pick up any boom
I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line)
P: Print, that is, print out a selected data. Normally p will run with parameter sed-n
S: Replace, can be directly replaced by work! Usually this s action can be paired with regular notation

1. Addressing: addressing is used to decide which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, SED processes all lines of the input file.

2.D, deleting rows

Example 1. [[Email protected] ~]# sed ' 2d '/etc/fstab

650) this.width=650; "style=" Float:none; "title=" 19.png "alt=" wkiol1x5jpeihogwaaezzjgjzwi419.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m00/73/46/wkiol1x5jpeihogwaaezzjgjzwi419.jpg "/>

Example 2. Delete all lines after/etc/fstab line fifth, $ represents the last line

[[Email protected] ~]# sed ' 5, $d '/etc/fstab

650) this.width=650; "style=" Float:none; "title=" 20.png "alt=" wkiom1x5isdg_4vwaaa6u7uvvhy376.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m00/73/49/wkiom1x5isdg_4vwaaa6u7uvvhy376.jpg "/>

3.P command: The command p is used to display the contents of the pattern space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the Select-N and command p appear simultaneously, sed prints the selected content.

Example 3.

[Email protected] ~]# sed-n '/uuid/p '/etc/fstab

650) this.width=650; "title=" 28.png "alt=" wkiom1x5jucjh6viaac4e6oq2vw765.jpg "src=" http://s3.51cto.com/wyfs02/M02/ 73/49/wkiom1x5jucjh6viaac4e6oq2vw765.jpg "/>

4.S command: Format: sed ' s/string to replace/new string/g ' (string to replace can use regular expression)

Example 4.1 replacing the UUID in/etc/fstab with Aaid

650) this.width=650; "style=" Float:none; "title=" 21.png "alt=" wkiom1x5ishjq7fpaaegyws1z3m117.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m01/73/49/wkiom1x5ishjq7fpaaegyws1z3m117.jpg "/>

Example 4.2 Remove all # and white-space characters from the beginning of the line in the/etc/selinux/config file that begin with #, followed by at least one white-space character

650) this.width=650; "style=" Float:none; "title=" 22.png "alt=" wkiol1x5jpfxzxzjaael3jdk7-s527.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m01/73/46/wkiol1x5jpfxzxzjaael3jdk7-s527.jpg "/>

5.first~step: Specifies the starting line, and the step size. The W command reads in a file content

Example 5.1 saves the odd line of the/etc/passwd file to/tmp/passwd, where sed-A is represented from the first row and matched with a span of 2 behavior

650) this.width=650; "style=" Float:none; "title=" 23.png "alt=" wkiol1x5jpfdtwnyaakav1qmgde356.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/73/46/wkiol1x5jpfdtwnyaakav1qmgde356.jpg "/>

650) this.width=650; "style=" Float:none; "title=" 24.png "alt=" wkiom1x5ishanwpfaafprhhilqw641.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m01/73/49/wkiom1x5ishanwpfaafprhhilqw641.jpg "/>

6.e Options: - e is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.

Example 6. Match "root" line first, then replace "root" with "ADM"

650) this.width=650; "style=" Float:none; "title=" 25.png "alt=" wkiol1x5jpfailt6aaeamdq0nc4630.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/73/46/wkiol1x5jpfailt6aaeamdq0nc4630.jpg "/>

7.i\ command: Thei\ command inserts new text in front of the current line.

Example 7. Insert test before line Fourth of/etc/passwd

650) this.width=650; "style=" Float:none; "title=" 26.png "alt=" wkiom1x5isgbnd98aacvjrggyge101.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/73/49/wkiom1x5isgbnd98aacvjrggyge101.jpg "/>

8.R command:the R command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file.

650) this.width=650; "style=" Float:none; "title=" 27.png "alt=" wkiom1x5isgiiyizaafqdelvwzc614.jpg "src="/HTTP/ S3.51cto.com/wyfs02/m02/73/49/wkiom1x5isgiiyizaafqdelvwzc614.jpg "/>

Some special commands for 9.sed are as follows:

Sed ' G '/etc/issue: Adds a blank line behind each line in the file;
Sed ' $!d '/etc/fstab: Keep the last line;
Sed '/^$/d; G '/etc/issue: ensure that the specified file has only one blank line behind each line;
Sed ' n;d '/etc/issue: reserved odd lines;
Sed-n ' 1! G;h, $p '/etc/issue
Sed ' $! n;$! D '/etc/issue

An explanation of the awk command


What is awk? AWK is the abbreviation for three names: Aho, Peter Weinberg and (Brain) Kernighan. Awk is an excellent tool for scanning and processing of styles. 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. The awk command can be entered at the command line or written in the awk script file. The line of text to be processed comes from a file, pipe, or standard input.

1. Basic syntax

awk [Options] ' program ' File File ...

awk [Options] ' pattern{action} ' file File ...

2. Input from File

2.1 The following is a demonstration of the/etc/passwd file, using awk to print a row containing the root

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/4B/wKiom1X5PlHDL6oeAAA_D9YvxAU886.jpg "title=" 29.png "alt=" Wkiom1x5plhdl6oeaaa_d9yvxau886.jpg "/>

2.2 Prints 1.3 Two fields of a row when the print file/etc/passwd a line containing the pattern root

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/48/wKioL1X5QaWCpHjkAAApE6fgsOA291.jpg "title=" 30.png "alt=" Wkiol1x5qawcphjkaaape6fgsoa291.jpg "/>

3. From the command input

The output of one or more Linux commands can be piped to awk for processing. The format is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/48/wKioL1X5Qkuz3ASqAAAsT2u3TDw650.jpg "title=" 3.png " alt= "Wkiol1x5qkuz3asqaaast2u3tdw650.jpg"/>

4.awk variables: Built-in variables and custom variables

4.1 Built-in variables

  • Fs:field seperator, field delimiter at input

  • Rs:record seperator, enter line delimiter

  • Ofs:output field seperator, Fields delimiter at output

  • Ors:outpput row seperator, line delimiter at output

  • Nf:numbers of field, number of fields

  • Nr:numbers of Record, number of rows; count all files together

  • FNR: Number of rows; count each file separately

  • ARGV: An array that holds the character of the command itself

  • ARGC: Save the number of arguments in the awk command

  • Filename:awk the name of the current file being processed

4.2 Customizable variables

    • -V Var_name=value

    • Variables can be defined in program;

    • You can customize variables by using the-V option on the command line;

    • Variable names are case-sensitive;

5.print function

You can also explicitly call the print function in the action section of the awk command in the form of {print}. The parameters of the print function can be variables , numeric values, or string constants. The string must be enclosed in double quotation marks. The parameters are separated by commas, and if there are no commas, all arguments are strung together. By default, a space comma is equivalent to the value in ofs .

Example. Use the date command to output time and to intercept the year and month through awk

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/48/wKioL1X5R-Kw_dIIAAAt6tg1HXA999.jpg "title=" 31.png "alt=" Wkiol1x5r-kw_diiaaat6tg1hxa999.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/48/wKioL1X5SEqh6_rbAAEw-8tW13o184.jpg "title=" 32.png "alt=" Wkiol1x5seqh6_rbaaew-8tw13o184.jpg "/>

6.printf function

The printf function returns a formatted string to the standard output, just like a printf statement in the C language. The printf statement includes a quoted control string, which may have several format descriptions and modifiers embedded in the control string. The control string is followed by a comma followed by a comma-delimited list of expressions. The printf function orchestrates the format of these expressions according to the instructions in the control string. Unlike the print function, printf does not wrap lines at the end of a line. Therefore, if you want to wrap, you must provide the escape character \ n in the control string.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/4B/wKiom1X5R9bgoNWZAAK-uTa7aPs189.jpg "title=" 33.png "alt=" Wkiom1x5r9bgonwzaak-uta7aps189.jpg "/>

Let's look at the following example

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/4B/wKiom1X5SIPieG1EAAA3vcPzOMc910.jpg "title=" 34.png "alt=" Wkiom1x5sipieg1eaaa3vcpzomc910.jpg "/>

For the output of the echo command, Linux is piped to awk. The printf function contains a control string. The percent semicolon prepares printf to print a 15-cell, left-aligned string that is sandwiched between two vertical bars and ends with a newline character. The dash after the percent semicolon indicates left alignment.

7. Field Separators

input field delimiter : awk memory variable fs. When using the default values for FS, awk separates fields with spaces or tabs

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/49/wKioL1X5VpvDCN5-AAA3Xj_XZbc551.jpg "title=" 35.png "alt=" Wkiol1x5vpvdcn5-aaa3xj_xzbc551.jpg "/>

Although the preceding-F has a specified delimiter:, it is later overridden by assigning a value to the variable OFS. This means that we can define delimiters by ofs this variable.












This article is from the "Genius from Repetition" blog, please be sure to keep this source http://laoli110.blog.51cto.com/9136611/1695390

Linux Sed,awk detailed

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.