Linux Sed&awk detailed

Source: Internet
Author: User
Tags case statement

Sed

Sed is one of the Three Musketeers for text processing. itself is a pipe command, you can add files, modify, delete, select and other operations.


Format: sed [-nrefi] [command] "text string"


Options:

-R: Supports extended regular expressions;

-N: Silent mode, (SED has a mode space and hold space, the default SED will save the result of execution to the pattern space, and the mode space by default is output on the screen, plus-N, block the contents of the mode space to the screen )

-f:/path/to/script_file: Reads the script from the specified file and runs

-E script1-e script2-e SCRIPT3: Specifies multiple scripts to run;

-I: Directly modify the source file; ( this is used with caution )


Address delimitation:

#: Specify Line

$: last line;

/regexp/: Any row that can be matched to by regexp;

\%regexp%: Ibid., except for the regexp boundary symbol;

/regexp/| :

\%regexp%| : Ignoring character case when matching;

Start line, terminating line:

#,/regexp/: Starting from # line, to the end of the line to which the first/regexp/is matched, all rows in the middle;

/regexp1/,/regexp2/: Starting from the first line that is matched to the/regexp1/, to the end of the line that is first matched to the/regexp2/, all rows in the middle;

#,+n: Starting with # lines, up to n rows down;

First~step: Specify the starting line, and the step size;

sed-n ' 1~2p '/etc/fstab show/etc/fstab odd lines


Edit commands for SED

D: Delete rows in the pattern space;

=: Display line number;

A \text: Append text

I \text: Insert text, support \ n implement multi-line insertion;

C \text: Replace the matching line with text;

P: Print the lines in the mode space;

s/regexp/replacement/: Replace the content that is matched by regexp to replacement;

G: global substitution; This is the same as vim.


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;


The above mentioned SED has a pattern space and hold space, below to explain the space to maintain the relevant commands (understand the line)

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;


Sed instance:

1. Delete the white space character at the beginning of all lines in the/tmp/grub.conf file;

Answer:sed-r ' s/^[[:space:]]+//g '/tmp/grub.conf


2. Remove all # and white-space characters from the beginning of the line at the beginning of #, followed by at least one white-space character, in the/etc/fstab file;

Answer:sed-r ' s/^#[[:space:]]+//g '/etc/fstab


3. Save the odd line of the/etc/fstab file as a/tmp/fstab.3;

Solution:sed ' 1~2w/tmp/fstab.3 '/etc/fstab


4.echo a file path to the SED command, take out its base name, and further, remove its path name;

Answer:

1.echo/etc/fstab | Sed-r ' [Email protected]*/([^/]+)/[email Protected]\[email protected] ' base name

2.echo/etc/fstab/| Sed-r ' [email protected] ([^/]+)/[email protected]@g ' path name


Awk


Brief introduction

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.

Awk has its name from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's surname. In fact, Awk does have its own language: The awk programming language, a three-bit creator has formally defined it as "style scanning and processing language." It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.


Usage

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

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


-F CHAR: Input delimiter


awk output

Print Item1, item2,...

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 "";


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


The awk default delimiter is a space.


Awk's syntax is very similar to the C language syntax,

Conditional expression:

Selector?if-true-expression:if-false-expression


awk built-in variables:

Fs:field seperator, enter the field delimiter (default space)

Rs:record seperator, enter line delimiter (default line break)

Ofs:output field seperator, the output separator (default space);

Ors:outpput row seperator, the line delimiter at output;


Nf:numbers of field, number of fields

Nr:numbers of Record, number of rows; All documents are counted together;

FNR: number of rows; Each file is counted separately;

ARGV: An array that holds the character of the command itself, awk ' {print $} ' 1.txt 2.txt, meaning argv[0] saves awk,

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

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


Mode

(1) Regexp: Format is/pattern/

Only the rows that are matched to the/pattern/are processed;

(2) Expression: expressions that satisfy a condition when the result is a non-0 or a non-empty string;

Only the rows that satisfy the condition are processed;

(3) Ranges: Line range, previous address delimitation,

Nr

Processing only rows within a range

(4) Begin/end: Special mode, executed only once before the program of the awk command is run (BEGIN) or after run (END);

(5) Empty: null mode, matching any row;


Control statements:

If-else

Format: if (condition) {then body} else {else body}


While

Format: while (condition) {while body}


Do-while Cycle

Format: do {Do-while body} while (condition)


For loop

Format: for (variable assignment; condition, iteration process) {for body}


Case statement

Syntax: switch (expression) {case VALUE or/rgeexp/: statement1; ... default:stementn}


Loop control

Break

Continue


Next

End the processing of the bank in advance and proceed to the next line;


Array

Traditional arrays: The index number starts at 1;

Associative arrays:

Array[index-expression]

Index-expression: Any string can be used; If an array element does not exist beforehand, awk automatically creates the element and initializes it to an empty string when referenced, so that the "index in array" format must be used to determine if an array exists;

a[first]= "Hello awk"

Print A[second]


To enumerate through each element of the group, you need to use a special structure like this:

For (var. array) {for Body} whose var iterates through the index of the array;


To delete an array element:

Delete Array[index]


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;



This article is from the "Wind Rhyme" blog, please be sure to keep this source http://chinalx1.blog.51cto.com/2334265/1699694

Linux Sed&awk detailed

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.