Shell and regular expressions

Source: Internet
Author: User
Tags egrep

Job One: organize regular expression Blogs

Have finished finishing.
Job Two: grep jobs (regular expressions and character processing)
Target file/etc/passwd, using the grep command or Egrep
1. All rows containing root are displayed:
[[email protected] ~]# grep-l ' root '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
2. Output any rows that contain bash, and also output the contents of the next two lines, followed by the row:
grep ' bash '/etc/passwd
Grep-a 1-b 1 ' bash '/etc/passwd
3. Shows how many lines contain nologin.
[Email protected] ~]# grep-c ' Nologin '/etc/passwd
21st
4. Shows that the rows contain root and the line number is output.
[[email protected] ~]# grep-n ' root '/etc/passwd or egrep-n ' root '/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
5. Show the file name
[[email protected] ~]# grep-rn ' root '/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

6. New user
Abominable
Abominate
Anomie
Atomize
Write regular expressions and match them up
[Email protected] ~]# Useradd abominable
[Email protected] ~]# Useradd abominate
[Email protected] ~]# useradd anomie
[Email protected] ~]# Useradd atomize
[Email protected] ~]# grep-we ' Abomina (bl|t) e|a (n|t) omiz?e '/etc/passwd--color

7. Built four users
Alex213sb
wpq2222b
Yh438pig
egon666
Egon

Filter out the user name consists of a letter + number + letter Line
8. Displays all filenames containing root in/etc directory
[[email protected] ~]# grep-rl ' root '/etc/
9. Filter out all comments and all blank lines in/etc/ssh/sshd_config
[Email protected] ~]# egrep-v "^$|^#"/etc/ssh/ssh_config or Grep-ev "^$|^[#;]"/etc/ssh/ssh_config

Job Three: SED job:/etc/passwd file as Template

1. Delete the first character of each line of the file.
[Email protected] ~]# head-10/etc/passwd >> test.txt
[email protected] ~]# cat Test.txt
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin

[Email protected] ~]# sed-i ' s/.//' test.txt or Sed-r ' s/^ (.) (. *) $/\2/' test.txt

[email protected] ~]# cat Test.txt
Oot:x:0:0:root:/root:/bin/bash
In:x:1:1:bin:/bin:/sbin/nologin
Aemon:x:2:2:daemon:/sbin:/sbin/nologin
Dm:x:3:4:adm:/var/adm:/sbin/nologin
P:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Ync:x:5:0:sync:/sbin:/bin/sync
Hutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Alt:x:7:0:halt:/sbin:/sbin/halt
Ail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Perator:x:11:0:operator:/root:/sbin/nologin
2. Delete the second character of each line of the file.
[Email protected] ~]# sed-i ' s/. /\2/' Test.txt
3. Delete the last character of each line of the file.
Sed-i ' s/(.) (. *) $//' Test.txt
4. Delete the second-to-last character of each line of the file.

5. Delete the second word in each line of the file.

6. Delete the second-to-last word in each line of a file.

7. Delete the last word in each line of the file.

8, swaps the first character and the second character of each line.

9, swaps the first character and the second word of each line.

10, swap the first word and the last word of each line.

11, delete all the numbers in a file.

12, remove all spaces at the beginning of each line.

13, replace any spaces that appear in the file with tabs.

14, enclose all uppercase letters in parentheses ().
Sed-r ' s/[a-z]/(&)/g '/etc/passwd
15, print 3 times per line.

16, only the first word of each line is displayed.

17, print the first word and the third word of each line.

18, use the command to get the format MM/YY/DD date format, combined with the pipeline, change it to MM;YY;DD format

Shell and regular expressions

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.