Chapter 4 sed commands and awk programming for Shell Learning

Source: Internet
Author: User
Tags control characters

Sed command

Sed only edits the copy of the original file in the buffer and does not change the source file. Therefore, to save the file, you must redirect it to another file.

Sed:

1. Sed [Option] 'sed command 'input file

2. Sed [Option]-F sed script file input file

3../SED script file input file

Among them, the SED script file 3 must start #! Bin/sed-F and so on

Option:

-N does not print all rows to the standard output. By default, the matching rows are printed before all rows are printed.

-E: Associate multiple sed commands

-F call the SED script file

Positioning text:

X indicates the specified row number.

Rows X and Y from X to Y

/Pattern/rows in the include Mode

/Pattern/contains rows in two modes

/Pattern/, X indicates the rows from the matched row with pattern to the row between X.

X,/pattern/from X row to line matching pattern

X, Y! Rows that do not contain the X and Y row numbers

Sed edit command:

P print matched rows

= Print the row number

A \ append text information after the row is located

I \ insert text information before locating the row number

D. Delete the row to be located.

C \ replace positioning text with new text

S replacement Mode

R read text from another file

W. Write text into a file

Y character conversion

Q. exit after the first pattern match is complete.

L display control characters

{} A group of commands executed on the locating line

N read the next input line and use the next command to process the new line

H. Copy the text in the mode buffer to the keep buffer.

H. append the text in the mode buffer to the keep buffer.

X swap mode buffer and keep buffer content

G copies the contents of the buffer to the mode buffer.

G append the content of the buffer to the mode buffer.

Example:

1.

Sed-n'/CLC/P' clc.txt

Find the rows with CLC in clc.txt and output the rows. If-N is not found, all contents will be output.

2.

Sed-n-e '/CLC/p'-e'/CLC/= 'clc.txt

Or sed-n'/CLC/{P; =} 'clc.txt

Or sed-n'/CLC/P;/CLC/='

Output matching row and row number

3.

Cat A. Sed

#! /Bin/sed-F

/CLC \./A \ # If it is changed to I \, It is inserted, that is, in the Matching Behavior

Append a line \

Append another line.

Chmod U + x a. Sed

./A. Sed clc.txt

Add two lines after the first line in clc.txt that appears CLC.

4.

Sed-n'/CLC/, $ P' clc.txt

Print the line matching CLC in clc.txt to the last line.

5.

Sed-n'2, 10! P'clc.txt

Printing is not 2 ~ Rows between 10 rows

6.

Cat A. Sed

#! /Bin/sed-F

/[CC] [ll] [CC]/d

Chmod U + x a. Sed

./A. Sed clc.txt

Delete rows with CLC (case-insensitive)

7.

Sed-n's/CLC/2P 'clc.txt

Replace the second matched CLC with ClC. Note that SED will only output the result (the replaced row) to the standard output without changing the original file.

In the replacement mode, P and-N are combined to print only the replaced rows.

8.

Sed-n's/CLC/W output.txt 'clc.txt

Output result to output.txt. W redirects the output to a file. No P is printed on the screen.

9.

Sed-n's/CLC/(&)/PG 'clc.txt

Replace CLC with (&) globally. & indicates CLC.

10.

Sed-n-e '/Cl \ {3 \} C/W output.txt'-E'/Cl \ {3 \} C/P' clc.txt

Output the line matching clllcto output.txt and to the screen

11.

Sed-n'/Cl \ {3 \} C/Q' clc.txt

Match the first clllc and print and exit

12.

Sed 'y/12345/ABCDE/'clc.txt

Replace 1 in clc.txt with A, 2 with B, and so on.

13.

Sed-N '1, $ l' clc.txt

Displays the content and control characters of clc.txt, and L is the display control character

Awk Programming

Three phases of awk:

1. Execution Code segment before reading the file (identified by begin)

2. input code segment when reading the input file

3. Execution Code segment after reading the input file (identified by end)

Awk execution method:

1. awk [-F domain separator] 'awk program Block' input file

2. awk-F awk script file input file

3./awk script file input file

Note that the 3 method must indicate the awk Or gawk path at the beginning, for example #! /Bin/awk-f or #! /Bin/gawk-F

Example:

1. awk mode matching

The awk consists of modes and actions. The mode tests whether the input row needs to perform the action and the Action executes the processing of the input row.

Execution Method 1:

[[Email protected] TMP] # Cat B

[[Email protected] TMP] # awk '/^ $/{print "this is a blank line"}' B

This is a blank line

This is a blank line

This is a blank line

This is a blank line

This is a blank line

Method 2:

[[Email protected] TMP] # Cat B. awk

/^ $/{Print "this is a blank line "}

[[Email protected] TMP] # awk-f B. awk B

This is a blank line

This is a blank line

This is a blank line

This is a blank line

This is a blank line

Method 3:

[[Email protected] TMP] # Which awk

/Bin/awk

[[Email protected] TMP] # Cat B. awk

#! /Bin/awk-F

/^ $/{Print "this is a blank line "}

[[Email protected] TMP] # chmod U + x B. awk

[[Email protected] TMP] #./B. awk B

This is a blank line

This is a blank line

This is a blank line

This is a blank line

This is a blank line

2. Records and Domains

A file consists of records and records are composed of fields. By default, a record is displayed in one row, and fields are separated by spaces or tabs.

[[Email protected] TMP] # Cat Tel

CLC, 1,234325

Clc1, 8, 258353

Clc3, 3, 234583

Clc4, 2,345534

[[Email protected] TMP] # awk 'in in {FS = ","; one = 1; two = 2} {print $1 "$ (one + two )} 'tel

234325 CLC

Clc1 258353

Clc3234583

Clc4 345534

3. Relational and Boolean operations

$0 indicates the record being processed, $1, $2... indicates the ID of the record's domain ,~ To match regular expressions ,!~ Is a non-matching Regular Expression (the regular expression must be)

[[Email protected] TMP] # awk 'in in {FS = ":"} {if ($3 >= 500 & $0 !~ /Nologin/) Print $0} '/etc/passwd

CLC: X: 500: 500: CLC:/home/clc3:/bin/bash

Clc3: X: 501: 502:/home/clc3:/bin/bash

Clc2: X: 502: 501:/home/clc2:/bin/bash

4. Expression

+,-, *,/, % (Modulo operation), ^ Or ** (multiplication), ++ X, X ++

[[Email protected] TMP] # Cat B

[[Email protected] TMP] # awk '/^ $/{print x ++}' B

0

1

2

3

4

[[Email protected] TMP] # awk '/^ $/{print ++ x}' B

1

2

3

4

5

5. System Variables

$ N nth domain, separated by FS

$0 records all domains, that is, the records currently processed

Number of argc command line parameters (the command itself is parameter 1)

Argv command line parameter Array

Location of the current file in the argind command line (starts with 0)

Convfmt digital conversion format

Environ environment variable Association Array

Description of the last system error in errno

Current filename file name

FS field delimiter

RS record delimiter

Specifies the delimiter of the output field of OFS. The default Delimiter is space.

ORS output record delimiter. The default value is line feed.

Ignorecase Boolean variable. If it is true, case insensitive.

Number of Domain records of NF

Current Nr records

Example 1:

[[Email protected] TMP] # awk 'in in {FS = ","; OFS = ";"; ors = "|"} {print NR, NF, $0, "\ n"} 'tel

1; 3; CLC, 1, 234325;

| 2; 3; clc1, 8, 258353;

| 3; 3; clc3, 3, 234583;

| 4; 3; clc4, 2,345534;

Example 2:

[[Email protected] TMP] # Cat a4.awk

#! /Bin/awk-F

Begin {

For (I in environ)

{Print I "=" environ [I];};

}

[[Email protected] TMP] #./a4.awk

Term = xterm

G_broken_filenames = 1

Dbus_session_bus_address = Unix: abstract =/tmp/logs-aqhrdkeqne, guid = 9fcb974e6fd4a8e074366f00509e212a

Gnome_shorttop_session_id = default

Shlvl = 2

Pwd =/tmp

Export top_session = default

Path =/usr/Kerberos/sbin:/usr/Kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin: /usr/sbin:/usr/bin:/usr/x11r6/bin:/root/bin

...

6. format the output.

Printf modifier:

-Left alignment

Step Size of the width field

. Prec number of digits to the right of the decimal point

Printf format OPERATOR:

% C ascii characters

% D integer

% E floating point number, scientific notation

% F floating point number

% O octal values

% S string

% X hexadecimal number

[[Email protected] TMP] # Cat Tel

CLC, 1,234325

Clc1, 8, 258353

Clc3, 3, 234583

Clc4, 2,345534

[[Email protected] TMP] # awk 'in in {FS = ","} {printf ("% 5.2f \ t % 5s \ t %-5d \ n", $2, $1, $3)} 'tel

1.00 CLC 234325

8.00 clc1 258353

3.00 clc3 234583

2.00 clc4 345534

7. built-in string functions

Gsub (R, S) Replace r with S in the input file. R is a/regular expression/or "replaced content", and S is "replaced content"

Gsub (R, S, T) r matches in the records specified by T, for example, T is $1

Index (S, T) returns the position of the first t of the string in S.

Length (s) returns the length of S.

Match (S, T) test whether S contains a string that matches T. If not, 0 is returned; otherwise, the distance from S header is returned.

Split (R, S, T) Splits R with T as the separator, and the result is saved to the S array. The number of split records is returned.

Sub (R, S, T) replaces the r that appears for the first time in T with S

Substr (R, S) returns the suffix starting from position s in string R.

Substr (R, S, T) returns the suffix of the string R starting from position s to T.

Example:

[[Email protected] TMP] # Cat

CLC

Clllc

CC

ABC

Cabcc

[[Email protected] TMP] # awk 'gsub (/Cl + C/, "KKK") {print $0} 'a

Kkk

Kkk

[[Email protected] TMP] # awk '{gsub (/Cl + C/, "KKK"); print $0} 'a

Kkk

Kkk

CC

ABC

Cabcc

Note the difference between grub {} and not {}

[[Email protected] TMP] # awk 'in in {STR = "Hello World"; print substr (STR, 3,6 )}'

LLO wo

8. PASS Parameters

[[Email protected] TMP] # Cat Tel

CLC, 1,234325

Clc1, 8, 258353

Clc3, 3, 234583

Clc4, 2,345534

[[Email protected] TMP] # Cat A. awk

#! /Bin/awk-F

NF! = Max

{Print ("the line" Nr "does no have" Max "filds ")}

Or:

[[Email protected] TMP] # Cat A. awk

#! /Bin/awk-F

{Print $0; If (NF! = Max) {print ("the line" Nr "does no have" Max "filds ")}}

[[Email protected] TMP] #./A. awk max = 2 FS = "," Tel

CLC, 1,234325

The line 1 does no have 2 filds

Clc1, 8, 258353

The line 2 does no have 2 filds

Clc3, 3, 234583

The line 3 does no have 2 filds

Clc4, 2,345534

The line 4 does no have 2 filds

9. Condition and loop statements

If else

For example, if (x ~ /[HH] El? O/) print x

While

Do while

For

Example:

[[Email protected] TMP] # Cat a2.awk

#! /Bin/awk-F

Begin {

For (I = 0; I <argc; I ++)

{

Printf ("Arg % d is % s \ n", I, argv [I]);

};

Printf ("there are % D Arg \ n", argc );

}

[[Email protected] TMP] #./a2.awk A1 A2 A3

Arg0 is awk

Arg1 is A1

Arg2 is A2

Arg3 is A3

There are 4 ARG

10. Array

Array [Index] = Value

For (index in array)

Example 1:

[[Email protected] TMP] # Cat a1.awk

#! /Bin/awk-F

Begin {

STR = "str1/str2/str3 ";

Print split (STR, result ,"/");

For (fild in result)

{

Print result [fild];

}

}

Example 2:

[[Email protected] TMP] # Cat a3.awk

#! /Bin/awk-F

Begin {

FS = ",";

If (argc> 2)

{

Name = argv [1];

Delete argv [1];

}

Else

{

While (! Name)

{

Print "Please enter a name :";

Getline name <"-"; # Read from standard input

}

};

}

{

If ($1 ~ Name)

{Print $1, $3 ;};

}

[[Email protected] TMP] # Cat Tel

CLC, 1,234325

Clc1, 8, 258353

Clc3, 3, 234583

Clc4, 2,345534

ABC, 5, 2343243

[[Email protected] TMP] #./a3.awk ABC Tel

ABC 2343243

[[Email protected] TMP] #./a3.awk Tel

Please enter a name:

ABC

ABC 2343243


This article is from the "flyclc" blog, please be sure to keep this source http://flyclc.blog.51cto.com/1385758/1540162

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.