Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Awk sed example activity

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Introduction to 1perfused

Sed can delete (delete), change (change), add (append), insert (insert), merge, exchange columns in a file, or read data into other files into a document, or substuite their strings, or convert them.

(tranfer) the letters in it, etc. For example, delete the continuous blank lines in the file into one line, replace the "local" string with "remote", convert the "t" letter to "T", combine line 10 data with 11 data, and so on.

To sum up, when sed reads a row of data from standard input and puts it into pattern space, sed edits the data in pattern space one by one according to the editing instructions of sed script, and then the

The result is sent to the standard output, and then the next line of data is read in. Repeat the above action until you have read all the data lines.

Summary, remember:

(1) sed always processes input in rows

(2) sed does not deal with the original file but the copy of the original file.

Command line overview:

The format of the sed editing instruction is as follows:

[address1 [, address2]] function[argument]

Where the address parameters address1 and address2 are the number of rows or regular expression string, indicating the row of data being edited, and the function parameter function [argument] is the default function of sed, indicating the edit performed.

action.

What are the function (function) parameters

The functions of all sed function parameters (see [chapter 4]) are described in the table on the following page.

Function parameter function

Label establishes a location in the script file where instructions refer to each other.

# create the solution

{} collection has instructions with the same address parameters.

! Function arguments are not executed.

= number of rows of data printed (line number).

A\ add data entered by the user.

B label jumps the executed instruction to the reference location established by:.

C\ replace data with data entered by the user.

D delete data.

D Delete the data before the first newline letter\ in the pattern space.

G copy data from hold space.

G add data from hold space to pattern space.

H copy materials from pattern space to hold space.

H add data from pattern space to hold space.

L print out the nonprinting character in l data with ASCII code.

I\ insert a column that adds user input.

N read the next piece of information.

N add the next piece of data to pattern space.

P print out the data.

P print the data before the first newline letter\ in the pattern space.

Q pops out of the sed editor.

R read the contents of another file.

S replace the string.

T label executes a replacement edit instruction first, and if it is replaced by Niu p >, the edit instruction is skipped to: label.

W write the data to its file.

X exchanges hold space and pattern space content.

Convert (transform) characters.

Although sed has only a few functions with basic editing functions described in the table above, the address parameters in instructions and the cooperation between instructions can also enable sed to complete most of the editing tasks.

2Pol 1 delete

(1) sed-e '1d' inputfile (delete the first line)

What about deleting line x? Delete lines x1, x2, and x3?

Sed-e 'xd' inputfile

Sed-e 'x1d'-e' x2d'-e 'x3d' inputfile

Of course, maybe there's a better way.

(2) sed-e'1J 3d' file (delete the first to third lines)

Think: delete lines n to m? That is,

Sed-e'n Md'file

Delete the first line to the last line

Sed-e'1 last line and last line of file # $

(3) sed-e'/ # / d' file (delete the line containing the'# 'sign)

Think: delete a line that contains the letter xx

Sed-e'/ xx/d' file

Think about it: delete all lines except the string xx

Sed-e'/ xxram file

(4) sed-e'/ word1/, / word2/d' file (delete the line from the word word1 to the word word2)

Sed-e'10 Word1According to file

Delete the line from line 10 to the line containing word1 in the file

Sed-e'/ word1/,10/d' file

Contrary to the match above, delete from the line containing word1 to line 10

(5) sed-e'/ t.delete t _ file (delete the line with two t)

Think about it: delete the row that contains the specified expression matching.

2.2 replace

Sed can replace strings, columns, or even data areas in a document. Where the function argument in the instruction that represents the replacement string is s, and the function argument in the instruction that represents the replacement column or data area > is c. The above situation is illustrated by the following three examples

.

* replacement of lines

(1) sed-e'1c\ #! / bin/more' file (replace the first line with #! / bin/more)

Think about it: replace line n with just do it

Sed-e'nc\ just do it' file

(2) sed-e'1J 10c\ I can do it' file (replace 1 to 10 lines with one line: I can do it)

Think: change to two lines (I can do it! Let's start)

Sed-e'1 I can do it 10c!\ nLet' "\" s start' file

Replacement of * character

(3) sed-e's word1 word 1 & word2/' file (replace the word of each line with the parameter s and combine at most two positional parameters. There are two special symbols in the parameter s of the function:

&: stands for pattern

\ n: represents the string in pattern enclosed by the nth\ (,\) (see [Appendix A]). For example

Sed-e's file W2 output W1 W2 from the place of 'W1Compare & W2Universe' W1

Sed-e's /\ (test\)\ (my\)\ (car\) / [\ 2\ 3\ 1] / 'file # result: [my car test]

* examples of flag parameters

Sed-e's file W1 file & W2 Compact g'

G: stands for replacing all matching items; here, all strings W1 in the file are replaced with strings W1 w2

Sed-e's file W1 file & W2 Compact 10'

M (10): replace the m matching string on the line; remember, it is the m matching string on the line

Sed-e's file W1 file & W2 Compact p 'Universe

P: replace the first string that matches W1 with W1 W2 and output to standard output.

Sed-e's file W1Compact & w2lide w2file'Universe

W filename: this parameter writes the replaced content to the file w2file and outputs the entire replaced file. Notice that what is written in w2file is just a replaced line. Sed's file W1max & w2gamble

The flag here is empty, so the first string that matches W1 is replaced with W1 W2, and the next one is not replaced.

* examples of application of location parameters

Sed-e'/ machine/s/phi/beta/g' file

Replace the "phi" string in the column containing the "machine" string in the file with the "beta" string

Sed-e '1jue 10s file W1max & w2max g'Universe

Replace the W1 string from 1 to 10 with the W1 w2 string.

Sed-e'1 mag le / s le le w 1max & w 2max g 'file

Replace the W1 string from 1 to the string else with the W1 w2 string.

The application of other position parameters is the same as before.

2.3 insertion of content

I

Basic format:

[address] I\ insert content filename

Word2)

Description:

The function parameter s represents the string in the substitute file. The instruction format is as follows:

[address1 [, address2]] s/pattern/replacemen/ [flag]

Sed-e'/ # / I\ words' file # insert a line words before the # character

Description:

The function argument here is I, which can only have one address parameter.

Sed-e '1DB I\ words' file

Add a words line before the first line

Cat "word" | sed-e'/ $/ .doc / g'# output word.doc

Add a suffix after word to output word.doc

The I parameter is exactly the opposite of the a parameter, which is inserted in front of the given content.

A

The format of parameter an is as follows:

[address] a\ filename

Sed-e'/ unix/a\ ' test.txt # add "" after the line containing unix

# the output is as follows:

Unix

Haha

In addition: sed-e'1 a\ hh' test.txt # adds the hh character after the first line.

2.4 Printing of text: P

Basic format:

[address1, [address2]] p

(1) sed-e'/ then/ p 'filename # prints all lines and reprints lines containing then

(2) sed-n'/ then/ p 'filename # prints only lines containing then

(3) sed-e'1J 3p 'filename # print all lines and repeat 1-3 lines

(4) sed-n'1J 3p 'filename # print 1-3 lines

(5) sed-n'/ if/,/fi/ p 'filename # print characters between if and fi

The p function is the print function of sed. Note the difference between the-e and-n parameters here. The-n parameter is generally used.

Replacement of 2.5 characters: y

For example:

(1) sed-e 'yUniqabc.Universe xyz.. filename

Replace the a letter in the file with x, b with y, c with z.

(2) sed-e'yUniverse abcUniverse ABC'filename

Convert lowercase abc to uppercase ABC

2.6 inversely execute the command:!

Basic format:

[address1 [, address2]]! Function parameter

Sed-e'/ 1996 Compact d' filename

Delete all rows except those containing 1996.

2.7 change the information in the document: C

Basic format:

[address1 [, address2]] c\ filename

The function parameter c is followed by the "\" character to indicate the end of this line, and the data entered by the user must be entered from the next line. If the data exceeds one line, add "\" at the end of each line.

Sed-e'/ zhengxh/c hhhh' filename

Indicates that the line containing the string zhengxh should be hhhh.

2.8 read the next line: n

Basic format:

[address1 [, address2]] n

Sed-n-e'/ echo/n'-e 'p' temp

Represents the output file, but if the line contains the string echo, the next line containing that string is output.

Sed-n-e 'n'-e 'p' filename

Output even lines in the text

3, the reuse of commands

There are three ways to execute multiple commands at a time:

(1) sed's words' filename W1max & W2max g; 1Universe I\ words' filename (use the; sign to separate the command, note that the-e parameter is not added before it)

(2) sed-e 'cmd1'-e' cmd2' filename (using multiple-e parameters)

one。 Replace

1. Magical transformation (use of the y command)

Code:

Sed'yUniverse letterless listlist target target LetterListAccording to filename

Code:

Cat filename

1234567890

2345678901

3456789012

4567890123

test

Replace 1 in the file with A

Replace 2 in the file with B

...

Replace 0 in the file with J

Code:

Sed'yGHIJ filename 1234567890 filename

ABCDEFGHIJ

BCDEFGHIJA

CDEFGHIJAB

DEFGHIJABC

Note that the transformation relationship is transformed according to the position of the two list. Y is an order of the Bureau, refusing to use the suffix flag/g.

List1:1234567890

List2:ABCDEFGHIJ

Let's make another transformation contrary to the previous example.

Code:

Sed'yUnix 0987654321 Universe ABCDEFGHIJ'filename

JIHGFEDCBA

IHGFEDCBAJ

HGFEDCBAJI

GFEDCBAJIH

two。 Replace the first match in each row

Code:

Sed's filename regexprip anyword

Sed's filename regexprhand anyword

For example:

Reference:

Cat filename

1234567890 2345678901

3456789012 4567890123

Sed's filename 5 / 5 / 5 / 5

1234 five 67890 2345678901

34 five 6789012 4567890123

3. Replace the nth (if any) match in each line

Code:

Sed "regexprhand anyword filename"

Cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

Give an example

Sed "Splash 4 / 4 / 8" filename

111111111111111111

222222222222222222

333333333333333333

4444444 four 4444444444

4. Replace all matches per row

Code:

Cat filename

1234567890 2345678901

3456789012 4567890123

Code:

For example:

Sed's Compact 3 / 3 / g 'filename

12 three 4567890 2 three 45678901

Three 456789012 456789012 three

two。 Line number processing

1. Add a line number to the file

Code:

Sed = filename | sed 'Nutters/\ nUniverse purpura'

Cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

Give an example

Code:

Sed = filename | sed'Nutters /\ nUniverse Universe 'filename

1:111111111111111111

2:222222222222222222

3:333333333333333333

4:444444444444444444

two。 Only add line numbers to the text lines in the file

Code:

Sed /. / = a | sed'/. / Nutters /\ nUniplex'

Give an example

Code:

Cat filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

Sed /. / = a | sed'/. / Nutters /\ nUniplex filename

1:111111111111111111

3:222222222222222222

4:333333333333333333

6:444444444444444444

three。 String flipping

Code:

Sed'/\ nUniverse GmbH /\ (.\)\ (. *\ n\) / &\ 2\ 1Universe

Give an example

Code:

Echo 1234567890 | sed'/\ nUniverse Genders /\ (.\)\ (. *\ n\) / &\ 2\ 1Universe

0987654321

four。 Selective output

1. Print document odd lines (interlaced output)

Code:

Sed'ntudd'

Sed'x'/ n / x'

Sed-n'ptern'

one

three

five

seven

two。 Print even lines (interlaced output)

Code:

Sed-n'nposition p'

Sed'1D / n / s / n / s / s

two

four

six

eight

3. Delete consecutive duplicate lines (be careful if the pattern space file is too large)

Code:

Sed'$! n; / ^\ (. *\)\ n\ 1 $/! P; D'

# use $! n beware of memory overflow

Give an example

Code:

Cat file

111111111111111111

222222222222222222

222222222222222222

333333333333333333

444444444444444444

444444444444444444

444444444444444444

444444444444444444

444444444444444444

Sed'$! n; / ^\ (. *\)\ n\ 1 $/! P; D'filename

111111111111111111

222222222222222222

333333333333333333

444444444444444444

4. Merge the upper and lower lines and separate them with spaces

Code:

Sed'$! n * * /\ n / /

Give an example

Code:

Cat file

1234567890

0987654321

After executing the order

1234567890 0987654321

5. Merge lines ending with the\ symbol with the following line and separate them with spaces (splicing line breaks)

Code:

Sed-e: a-e'/\\ $/ N; s /\\ n / /; ta'

Give an example

Code:

Cat filename

1 1111111111111111111111\

2 222222222222222222

3 3333333333333333\

4 444444444444444444

Sed-e: a-e'/\\ $/ N; s /\\ n / /; ta' filename

1 111111111111111111 2 222222222222222222

3 333333333333333333 4 444444444444444444

6. Splice lines by keyword

If a line begins with =, merge to the previous line and replace = as a space

Code:

Sed-e: a-e'$! n * * s /\ n * /; ta'-e'P * *

Give an example

Code:

Cat file

111111111111111111

222222222222222222

= 333333333333333333

444444444444444444

Sed-e: a-e'$! n countries /\ n countries / /; ta'-e'Pten D'filename

111111111111111111

222222222222222222 333333333333333333

444444444444444444

7. Outputs the next line of the matching line

Code:

Sed-n'/ regexpr/ {ntterp;} 'filename

Give an example

Code:

Cat filename

1 111111111111111111

2 222222222222222222

3 333333333333333333

4 444444444444444444

Sed-n'/ ^ 3 / {ntterp;} 'filename

4 444444444444444444

8. Display the line number of the matching line and output the uplink, matching line, and downline of the matching line

Sed-n-e'/ regexpr/ {=; x-t1 / n-e'- e h

Give an example

Code:

Cat filename

1 111111111111111111

2 222222222222222222

3 333333333333333333

4 444444444444444444

Sed-n-e'/ ^ 3 / {=; x-e h filename-1 / n-e'/ ^ 3 / {=

3 # match the line number of the line

2 2222222222222222222222 # previous line

3 33333333333333333333 # matching lines

4 444444444444444444444444 # next line

9. Delete a keyword matching line in a flag area of a document

Delete the line containing myword in the block that starts from being to the end of end in the document

Code:

Sed'/ ^ begin/,/ ^ end/ {/ myword/d;} 'filename

Reference:

Cat filename

Myword

Begin

Myword

Number!

Myword

Number!

Myword

Number!

Myword

Number!

End

Myword

Number!

test

Reference:

Myword

Begin

Number!

Number!

Number!

Number!

End

Myword

Number!

five。 String parsing

1. Parse two substrings from the string (the first 2 characters and the last 9 characters)

Code:

Echo "WeLoveChinaUnix" | sed-e'Humbago /\ (..\). * /\ 1Universe Xanthabash. * (.\ {9\}\) $/\ 1Unitablis /\ n /'

We ChinaUnix

two。 Decompose the date string

Code:

Echo 20030922 | sed's /\ (....\)\ (..\)\ (..\) /\ 1\ 2\ 3Compact'| read year month day

Echo $year $month $day

2003 09 22

# #

Cat file1

-- Code-- account number

101 aa

102 bb

103 cc

Cat file2

-code name-name

101 aaaa

103 bbbb

104 dddd

Awk 'NR==FNR {a [$1] = $2} NR > FNR {printf "%-10s\ t% muri 10s\ t% Mustco 10s\ n", $1tex a [$1], $2sort delete a [$1]} END {for (i in a) printf "%-10s\ t% color 10s\ n", iQuery a [I], ""}' 12 | sort-k 1 "

-n

Cat a

1399946_1399944_1399943_1399942_1399941_1399940_1399939_1399938_1399937_1399936_1399935_1399934_1399933_1399932_1399931_1399930

1399946_1399944_1399943_1399942_1399941_1399940_1399939_1399938_1399937_1399936_1399935_1399934_1399933_1399932_1399931_1399930

Awk 'BEGIN {RS= "_"} {print $1 | "sort"} 'a

1399946

1399944

1399943

#

Sed-n'/ wrwq/a\ shenzhen' myfile insert a line after wrwq, insert a shenzhen line

Sed-n'/ wrwq/i\ shenzhen' myfile inserts the line before wrwq

On line 2 of b.txt

Before

Insert "xxx"

Sed'2 iCompact xxx' b.txt

On line 2 of b.txt

After that

Insert "xxx"

Sed'2a Compact xx' b.txt

On lines 2 and 3 of b.txt

After that

Insert "xxx"

Sed'2 and 3a bat xxx' b.txt

On lines 2 and 3 of b.txt

Before

Insert "xxx"

Sed'2 and 3 iCompact xxx' b.txt

On the second to last lines of b.txt

Before

Insert "xxx"

Sed '2je $iCompact xxx' b.txt

[root@dbtest1 ~] # cat 1 "as shown in the file, the merged file is required to be two lines,"

one

two

three

four

five

six

[root@dbtest1 ~] # awk'{if (NR%3==0) printf ("% s\ n", $0); else printf ("% s", $0)}'1

1 2 3

4 5 6

[root@dbtest1 ~] # awk'{aqtlineache0 alternate getlineage baccalaure0}'1

1 2 3

4 5 6

[root@dbtest1 ~] # cat 1 | xargs-n3

1 2 3

4 5 6

[root@dbtest1 ~] # sed'Ntern / g'1

1 2 3

4 5 6

#

#

Delete duplicate lines

In awk, you can use! A [$0] + is used as a condition to process recurring rows.

[root@dbtest1 ~] # cat 2

111111111111111111

222222222222222222

222222222222222222

333333333333333333

444444444444444444

444444444444444444

444444444444444444

444444444444444444

444444444444444444

[root@dbtest1 ~] # awk'! a [$0] +'2

111111111111111111

222222222222222222

333333333333333333

444444444444444444

[root@dbtest1 ~] # sed'$! n; / ^\ (. *\)\ n\ 1 $/! P; D' 2

111111111111111111

222222222222222222

333333333333333333

444444444444444444

[root@dbtest2 ~] # more c

1 2

3 4

5 6

[root@dbtest2 ~] # sed': a Ting Nten s /\ n / / g Tan Ba Teng c

1 2 3 4 5 6

For awk'! a [$3] + +', you need to know 3 knowledge points.

1. Awk array knowledge, it doesn't make sense to ing

2. The basic command format of awk: awk 'pattern {action}'

When action is omitted, the default action is {print}, for example, awk'1' is awk'1 {print}'

3. The form of var++: read the value of the var variable first, and then + 1 for the varvalue

With data

1 2 3

1 2 3

1 2 4

1 2 5

For example, for awk'! a [$3] +'

When awk processes the first row: read the a [$3] value first and then increment it. A [$3] means that the a [3] value is empty (0), that is, awk'! 0percent, that is, awk '1percent, that is, awk' 1 {print}'.

When awk processes the second row: read the a [$3] value first and then increment it. A [$3] means 1, that is, awk'! 1, awk'0, or awk'0 {print}'.

.

The final effect is to print $3 lines that appear for the first time, that is, to remove $3 duplicate lines.

#

#

Separator

[root@dbtest1 ~] # more a

[aa]

Aa1

Dd2

Dd3

Dd4

[bb]

Dd5

Dd6

Dd7

Xx8

Dd9

5510

[root@dbtest1 ~] # awk-vRS= "\ n\ n" / aa/' a

[aa]

Aa1

Dd2

Dd3

Dd4

[root@dbtest1 ~] # awk'{if ($0! ~ / ^ $/) print;else exit} 'a

[aa]

Aa1

Dd2

Dd3

Dd4

[root@dbtest1 ~] # awk-vRS= "[" / aa/ {print RS$0}'a

[aa]

Aa1

Dd2

Dd3

Dd4

[root@dbtest1 home] # more c

1399946_1399944_1399943_1399942_1399941_1399940_1399939

[root@dbtest1 home] # awk 'BEGIN {RS= "_"} {print $1 | "sort"}' c

1399939

1399940

1399941

1399942

1399943

1399944

1399946

#

#

String parsing

1. Parse two substrings from the string (the first 2 characters and the last 9 characters)

Code:

Echo "WeLoveChinaUnix" | sed-e'Humbago /\ (..\). * /\ 1Universe Xanthabash. * (.\ {9\}\) $/\ 1Unitablis /\ n /'

We ChinaUnix

two。 Decompose the date string

Echo 20030922 | sed's /\ (....\)\ (..\)\ (..\) /\ 1\ 2\ 3Compact'| read year month day

Echo $year $month $day

2003 09 22

[root@dbtest1 home] # more 1

-daihao-zhanghao

101 aa

102 bb

103 cc

[root@dbtest1 home] # more 2

-daihao-zhanghao

101 aaaa

103 bbbb

104 dddd

[root@dbtest1 home] # awk 'NR==FNR {a [$1] = $2} NR > FNR {printf "%-10s\ t% sort 10s\ t% sort 10s\ n", $1mai a [$1], $2th delete a [$1]} END {for (i in a) printf "%-10s\ t% words 10s\ n", iQuery a [I], ""}' 2 | sort-k 1-n

-daihao-zhanghao-zhanghao

101 aa aaaa

102 bb

103 cc bbbb

104 dddd

#

#

Awk array

Summation and product of meter distance series

[root@dbtest1 home] # more 3

1 2 3

4 5 6

7 8 9

[root@dbtest1 home] # awk 'NF==3 {lump _ 0 ~ (th)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report