Linux notes () Contact and expansion of SED and awk

Source: Internet
Author: User

sed exercises

Copy/etc/passwd to/root/test.txt, print all lines with SED

[Email protected] sed]# sed-n ' 1, $p ' passwd


Print 3 to 10 lines of test.txt

[[email protected] sed]# sed-n ' 3,10 ' P passwd


Print the line containing ' root ' in test.txt

[[email protected] sed]# sed-n '/root/' P passwd


Delete Test.txt 15 rows and all subsequent rows

[[Email protected] sed]# sed ' 15,$ ' d passwd


Delete rows containing ' bash ' in Test.txt

[[Email protected] sed]# sed '/bash/' d passwd


Replace ' root ' as ' Toor ' in Test.txt

[[Email protected] sed]# sed ' s/root/toor/g ' passwd


Replace Test.txt '/sbin/nologin ' as '/bin/login '

[[Email protected] sed]# sed ' s#sbin/nologin#bin/login#g ' passwd


Delete all the numbers in rows 5 through 10 in Test.txt

[[Email protected] sed]# sed ' 5,10s/[0-9]//g ' passwd


Delete all special characters in test.txt (except for numbers and uppercase and lowercase letters)

[[Email protected] sed]# sed ' s/[^a-za-z0-9]//g ' passwd


Swap the first and last words in the test.txt position

[Email protected] sed]# sed ' s/\ (^[a-za-z][a-za-z]*\) \ ([^a-za-z].*\) \ ([^a-za-z]\] \ ([a-za-z][a-za-z]*$\)/\4\2\3\1/' passwd bash:x:0:0:root:/root:/bin/root-za-z]*$\)/\4\2\3\1/' passwd


Replace the first number and the last word that appear in the test.txt position

[[Email protected] sed]# sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\] \ ([^0-9].*\) \ ([^a-za-z]\] \ ([a-za-z][a-za-z]*$\) #\1 \5\3\4\2# ' passwd


Move the first number in the Test.txt to the end of a line

[[Email protected] sed]# sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\] \ ([^0-9].*$\) #\1\3\2# ' passwd


Test.txt 20 to the end of the line Plus ' AAA: '

[[Email protected] sed]# sed ', $s/^.*$/aaa:&/' passwd


SED expansion

    1. How to intercept a specific line in a file

[email protected] sed]# cat Test.txt

Ert

Fff

**

[ABCFD]

123

324

444

[Rty]

**

Fgfgf

How to intercept [ABCFD] to [rty] this paragraph?

Answer: [[email protected] sed]# sed-n '/\[abcfd\]/,/\[rty\]/p ' test.txt


2.sed how to convert uppercase and lowercase letters

(1) Turn all lowercase into uppercase

[[Email protected] sed]# sed ' s/[a-z]/\u&/g ' test.txt

(2) Turn all uppercase letters into lowercase

[[Email protected] sed]# sed ' s/[a-z]/\l&/g ' test.txt

(3) Capitalize the first lowercase letter of each word

[[Email protected] sed]# sed ' s/\b[a-z]/\u&/g ' test.txt


3.sed the last number in a row of a file

[email protected] sed]# cat Test.txt

Ert

Fff

**

[ABCFD]

123

324

444

[Rty]

**

Fgfgf

[[Email protected] sed]# sed ' s/\ (^[0-9].*\)/\1 12/' Test.txt


4. How to use SED to print rows from 1 to 100 lines containing a string

[email protected] sed]# cat Test.txt

Ert

Fff

**

[ABCFD]

123

324

444

[Rty]

**

Fgfgf

Answer: [[email protected] sed]# sed-n ' 1,100{/fd/p} ' test.txt

Fff

[ABCFD]

Fgfgf

Multi-character Support:

[[email protected] sed]# sed-n ' 1,100{/ert/p;/1./p} ' test.txt

Ert

123


5.sed Delete the next line of a keyword to the last line


awk Exercises

Print the entire test.txt with awk (the following is done with the awk tool for Test.txt)

[[email protected] sed]# awk ' {print $} ' passwd


Find all rows that contain ' bash '

[Email protected] sed]# awk '/bash/' passwd


Use ': ' as a delimiter to find a line with a third paragraph equal to 0

[Email protected] sed]# awk-f ': ' $3==0 ' passwd


Use ': ' as a delimiter to find the first line of ' root ' and replace the ' root ' of that segment with ' Toor ' (which can be used together with SED)

[Email protected] sed]# awk-f ': ' $1==root ' |sed ' s/root/toor/g ' passwd


Use ': ' As a delimiter to print the last paragraph

[[email protected] sed]# awk-f ': ' {print $NF} ' passwd


Print all rows with a number of rows greater than 20

[Email protected] sed]# awk ' nr>20 ' passwd


Use ': ' As a delimiter to print all third paragraphs less than the fourth paragraph

[Email protected] sed]# awk-f ': ' $3<$4 ' passwd


Use ': ' As a delimiter, print the first paragraph and the last paragraph, and the middle with ' @ ' connection (for example, the first line should be the form of ' [email Protected]/bin/bash ')

[[email protected] sed]# awk-f ': ' {' Print $ ' @ ' $NF} ' passwd


Use ': ' As a delimiter to add the fourth paragraph of the entire document, summing

[Email protected] sed]# awk-f ': ' {(tot=tot+$4)}; END {print tot} ' passwd


Copy/etc/passwd to/root/test.txt, print all lines with SED

[Email protected] sed]# sed-n ' 1, $p ' passwd


Print 3 to 10 lines of test.txt

[[email protected] sed]# sed-n ' 3,10 ' P passwd


Print the line containing ' root ' in test.txt

[[email protected] sed]# sed-n '/root/' P passwd


Delete Test.txt 15 rows and all subsequent rows

[[Email protected] sed]# sed ' 15,$ ' d passwd


Delete rows containing ' bash ' in Test.txt

[[Email protected] sed]# sed '/bash/' d passwd


Replace ' root ' as ' Toor ' in Test.txt

[[Email protected] sed]# sed ' s/root/toor/g ' passwd


Replace Test.txt '/sbin/nologin ' as '/bin/login '

[[Email protected] sed]# sed ' s#sbin/nologin#bin/login#g ' passwd


Delete all the numbers in rows 5 through 10 in Test.txt

[[Email protected] sed]# sed ' 5,10s/[0-9]//g ' passwd


Delete all special characters in test.txt (except for numbers and uppercase and lowercase letters)

[[Email protected] sed]# sed ' s/[^a-za-z0-9]//g ' passwd


Swap the first and last words in the test.txt position

[Email protected] sed]# sed ' s/\ (^[a-za-z][a-za-z]*\) \ ([^a-za-z].*\) \ ([^a-za-z]\] \ ([a-za-z][a-za-z]*$\)/\4\2\3\1/' passwd bash:x:0:0:root:/root:/bin/root-za-z]*$\)/\4\2\3\1/' passwd


Replace the first number and the last word that appear in the test.txt position

[[Email protected] sed]# sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\] \ ([^0-9].*\) \ ([^a-za-z]\] \ ([a-za-z][a-za-z]*$\) #\1 \5\3\4\2# ' passwd


Move the first number in the Test.txt to the end of a line

[[Email protected] sed]# sed ' s#\ ([^0-9][^0-9]*\) \ ([0-9][0-9]*\] \ ([^0-9].*$\) #\1\3\2# ' passwd


Test.txt 20 to the end of the line Plus ' AAA: '

[[Email protected] sed]# sed ', $s/^.*$/aaa:&/' passwd




Print the entire test.txt with awk (the following is done with the awk tool for Test.txt)

[[email protected] sed]# awk ' {print $} ' passwd


Find all rows that contain ' bash '

[Email protected] sed]# awk '/bash/' passwd


Use ': ' as a delimiter to find a line with a third paragraph equal to 0

[Email protected] sed]# awk-f ': ' $3==0 ' passwd


Use ': ' as a delimiter to find the first line of ' root ' and replace the ' root ' of that segment with ' Toor ' (which can be used together with SED)

[Email protected] sed]# awk-f ': ' $1==root ' |sed ' s/root/toor/g ' passwd


Use ': ' As a delimiter to print the last paragraph

[[email protected] sed]# awk-f ': ' {print $NF} ' passwd


Print all rows with a number of rows greater than 20

[Email protected] sed]# awk ' nr>20 ' passwd


Use ': ' As a delimiter to print all third paragraphs less than the fourth paragraph

[Email protected] sed]# awk-f ': ' $3<$4 ' passwd


Use ': ' As a delimiter, print the first paragraph and the last paragraph, and the middle with ' @ ' connection (for example, the first line should be the form of ' [email Protected]/bin/bash ')

[[email protected] sed]# awk-f ': ' {' Print $ ' @ ' $NF} ' passwd


Use ': ' As a delimiter to add the fourth paragraph of the entire document, summing

[Email protected] sed]# awk-f ': ' {(tot=tot+$4)}; END {print tot} ' passwd


AWK expands

    1. Use of the Gsub function in awk

awk ' Gsub (/www/, "abc") '/ETC/PASSWD//passwd file replace all www with ABC

2. Merge the same lines in the file into one line

Paste filename1 filename2

Cat A.txt
1 2 3
4 5 6
a b c

Cat B.txt
3 2 1
6 5 4
c B a

the paste a.txt b.txt result is
1 2 3 3 2 1
4 5 6 6 5 4
a b c C b a

If you want to connect at two file connections with a specified character, you can also use-D to specify
paste-d ' + ' a.txt b.txt
result is
1 2 3+3 2 1
4 5 6+6 5 4
a b c+c b a


Awk's more detailed usage : http://www.cnblogs.com/emanlee/p/3327576.html

Linux notes () Contact and extension of sed and 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.