Linux sed awk

Source: Internet
Author: User

Sed:

A non-interactive stream editor that modifies the text flowing through it through a variety of transformations and prints the output to the screen without changing the file itself, including deleting, finding, replacing, inserting, adding, or reading data from other files.

Usage scenarios:

Too large text, regular text modification, faster file processing speed, regular modification of difficult text

Grammar:

sed [parameters] Command directory

Working mechanism:

Reads one line of text to the mode space, finishes processing in it, and outputs the processing results to the standard output device

Common parameters:

-R supports extended regular expression syntax

-N Use Quiet mode, only the line that has been specially processed by SED will be listed after use

-e Edit the action directly on the instruction mode

-F writes a document for the SED action, and-f filename to perform the SED action within the filename

-I directly modifies the contents of the read file instead of the screen output

Command:

[N1[,N2]] function

N1,N2: Not likely to exist, generally represents ' selected number of rows '

Command options:

A new, can pick up the string, and the string will appear in the next line

c instead, you can pick up strings, which can replace rows between n1,n2

D Delete

I insert, followed by a string that appears on the new line

P Print Mode space row

P Prints the first line that matches

G Global Substitution

S/old/new replacing regular expression old with new--generally used with g

= line number is displayed

w/path/to/somefile: Save the specified content to the file specified by the/path/to/somefile path;

R/path/from/somefile: Insert all the contents of another file at the specified location of the file, complete the file merge;

[[email protected] bashtest]# nl passwd |sed ' s/user001/usernew001/g '

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/6C/wKioL1X9cRfQBmjVAAEtvRkxj4g404.jpg "title=" QQ picture 20150919222110.png "alt=" Wkiol1x9crfqbmjvaaetvrkxj4g404.jpg "/>

[[email protected] bashtest]# nl passwd |sed  ' 2,5d '       1root:x:0:0:root:/root:/bin/bash     6sync:x:5:0:sync:/sbin:/bin/sync      7shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown     8halt:x:7:0 :halt:/sbin:/sbin/halt[[email protected] bashtest]# nl passwd |sed  ' P '       1root:x:0:0:root:/root:/bin/bash     1root:x:0:0:root:/root:/ Bin/bash     2bin:x:1:1:bin:/bin:/sbin/nologin     2bin:x :1:1:bin:/bin:/sbin/nologin     3daemon:x:2:2:daemon:/sbin:/sbin/nologin      3daemon:x:2:2:daemon:/sbin:/sbin/nologin[[email protected] bashtest]# sed  -i  ' s/\:/\#/g '  passwd [[email protected] bashtest]# cat passwd  Root#x#0#0#root#/root#/bin/bashbIn#x#1#1#bin#/bin#/sbin/nologindaemon#x#2#2#daemon#/sbin#/sbin/nologinadm#x#3#4#adm#/var/adm#/sbin/nologinlp#x #4 #7#lp#/var/spool/lpd#/sbin/nologinsync#x#5#0#sync#/sbin#/bin/syncshutdown#x#6#0#shutdown#/sbin#/sbin/ Shutdown
  • Advanced command:

  • H: Use the content of the pattern space to cover the content of keeping the space;

  • H: Append the contents of the pattern space to the content in the holding space;

  • G: Take its content from the hold space and overwrite it with the contents of the pattern space;

  • G: Take the content from the hold space and append it to the content in the pattern space;

  • X: Exchanging in the space of keeping and pattern;

  • N: Reads the next line from the matching row to the pattern space (overwrites the original content in the pattern space);

  • N: Reads the next line to the pattern space of the matched row, appended to the original content in the pattern space;

  • D: Delete the contents of the pattern space;

  • D: Delete the first row in multi-line mode space;

  • Note: Command function can be used! Reverse; semicolons can be used to separate scripts;

[[Email protected] bashtest]# sed ' G ' passwd root#x#0#0#root#/root#/bin/bashbin#x#1#1#bin#/bin#/sbin/nologindaemon#x     #2 #2#daemon#/sbin#/sbin/nologin[[email protected] bashtest]# nl passwd |sed ' n;d ' 1root#x#0#0#root#/root#/bin/bash 3daemon#x#2#2#daemon#/sbin#/sbin/nologin 5lp#x#4#7#lp#/var/spool/lpd#/sbin/nologin 7shutdown#x#6#0#shutdown#/sbi    N#/sbin/shutdown 9mail#x#8#12#mail#/var/spool/mail#/sbin/nologin 11games#x#12#100#games#/usr/games#/sbin/nologin 13nobody#x#99#99#nobody#/#/sbin/nologin

Common Regular Expressions:

^ Match Line start

$ match end of line

. Match any non-newline character

\(.. \) Save matching characters

x\{n\} repeat character x,n times

X\{m,\} repeat character x, at least m times

AWK: column-based text Processing tool, which he thinks is composed of words and whitespace characters

Format:

awk ' condition type 1{action 1} condition type 2{action 2} ' filename

awk [options] ' program ' File file ...

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

key points:

(1) Each item is separated by commas, and the output is separated by an output delimiter;

(2) Each item of the output can be a string or numeric value, a field of the current record, a variable, or an expression of awk, and the value will be implicitly converted to a string after the output;

(3) Print after item if omitted, equivalent to print $; output blank, use Pirnt "";

Operator:

Operator Description

= + = = *=/=%= ^= **= Assignment

?:C-conditional expression

|| Logical OR

&& Logic and

~ ~! match Regular expressions and mismatched regular expressions

< <= > >= = = = relational operator

Space Connection

+- Add, subtract

*/& multiply, divide and seek surplus

+ - ! unary Plus, minus and logical non-

^ * * exponentiation

+ +- -increase or decrease, as prefix or suffix

$ Field Reference

In array members



Environment variables:

Variable description

$n The nth field of the current record, and the fields are separated by FS.

A complete input record.

ARGC The number of command-line arguments.

Argind The location of the current file in the command line (starting at 0).

The ARGV contains an array of command-line arguments.

CONVFMT Number conversion format (default is%.6g)

ENVIRON An associative array of environment variables.

ERRNO A description of the last system error.

FieldWidths Field width list (separated by Space key).

FileName The current file name.

FNR with NR, but relative to the current file.

The FS field delimiter (the default is any space).

IGNORECASE If true, the matching of the case is ignored.

NF The number of fields in the current record.

NR current record count.

OFMT The output format of the number (the default value is%.6g).

OFS the Output field delimiter (the default value is a space).

ORS The output record delimiter (the default value is a newline character).

Rlength The length of the string that is matched by the match function.

The RS record delimiter (default is a line break).

Rstart The first position of a string that is matched by the match function.

Subsep array subscript delimiter (default is/034).

3. The printf command of awk

Use format for commands: printf format, item1, item2,...

Points:

(1) to specify format;

(2) will not be automatically wrapped, if you want to change the line, you need to give \ n

(3) format is used to specify its output format for each subsequent item;

The format indicator is preceded by%, followed by a character:

%c: The ASCII code that displays the characters;

%d,%i: decimal integer;

%e,%e: The scientific counting method shows the numerical value;

%f: Displays floating-point numbers;

%g,%g: Displays values in scientific notation format or floating-point number format;

%s: Display string;

%u: Displays unsigned integers;

Percent: show% itself;

Modifier:

#: Display width

-: Align Left

+: Display symbols for numeric values

. #: Value Accuracy


4. awk Output Redirection

Print Items > Output-file

Print Items >> output-file

Print Items | Command


Special File Descriptor:

/dev/stdin: Standard input

/dev/stdout: Standard Output

/dev/stderr: Error Output

Awk's built-in functions

Split (String,array[,fieldsep[,seps]):

Function: The string represented by FIELDSEP is sliced as a delimiter, and the result of the slice is saved to an array with the array name, and the subscript is starting from 1;

Length (String)

Function: Returns the length of a given string

SUBSTR (String,start[,length])

Function: Take a substring from a string, from start to the starting position as a substring of length;

[[email protected] bashtest]# last-n 5 |awk ' {print $ "\ T" $ $} ' Root211.161.27.115root61.182.229.120root61.182.229.120root180.91.225.119root180.91.225.119wtmptue[[email protected] bashtest]# [[email protected] bashtest]# awk ' {print substr ($1,6)} ' passwd x#0#0#root#/root#/bin/bash#1#1# Bin#/bin#/sbin/nologinn#x#2#2#daemon#/sbin#/sbin/nologin#3#4#adm#/var/adm#/sbin/nologin[[email protected] bashtest]# nl passwd |awk ' {print length} ' 383946


Linux sed awk

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.