Use of SED tools and regular expressions (Shell fourth day)

Source: Internet
Author: User
Tags save file

Sed tool "streaming editor"

--Non-interactive, filter based on pattern matching and modify text
--progressive processing and output of the results to the screen
--can achieve the output of the text, delete, replace, copy, cut, import, export and other operations

Command format:
1) Pre-command | sed [options] ' conditional directives ' "using pipelines"
2) SED [option] ' conditional directive ' file: ..

Common options:
-N Masks the default output, and the default SED outputs the entire contents of the read document

-I sed directly modifies the source file, the default SED only temporarily modifies the file through memory, the source file has no effect
[Normally, the SED command does the processing by outputting the results of the operation (including printing, deleting, etc.) to the current terminal screen without making any changes to the original file]

-R (Allow SED to support extended regular)

Cases:
-N
View File/etc/hosts line 1th
[Email protected] ~]# sed-n ' 1p '/etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

View/etc/passwd 3-6 rows
[Email protected] ~]# sed-n ' 3,6p '/etc/passwd
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

View files/etc/passwd lines 3rd and 6th
[Email protected] ~]# sed-n ' 3p;6p '/etc/passwd
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync

-I.
Delete files for deletion "actually no modification to files"
[[Email protected] ~]# sed ' d '/wooo.txt
[Email protected] ~]# Cat/wooo.txt
Thekncxn;zcnk
Dagahao the
Wojiaozazahuithe KOA
Modify source file "use with caution"
[Email protected] ~]# sed-i ' d '/wooo.txt
[Email protected] ~]# Cat/wooo.txt

II) Understanding the conditions of the SED tool

sed [options] ' conditional directives ' file: ..

1) SED commands can be conditionally matched using line numbers or regular:

Sed-n ' 3p '/etc/passwd print line 3rd

Sed-n ' 3,5p '/etc/passwd print line 3rd to 5th

Sed-n ' 3p;5p '/etc/passwd print lines 3rd and 5

Sed-n ' 3,+10p '/etc/passwd print 3rd and subsequent 10 lines

Sed-n ' 1~2p '/etc/passwd print odd lines

Sed-n ' 2~2p '/etc/passwd print even lines

2) Regular case

"Use//To find out what you need to search for"
Sed-n '/root/p '/etc/passwd print the row containing the root

Sed-n '/bash$/p '/etc/passwd print the line at the end of bash [user who can log in]

III) instruction set

"Print" p
Sed-n ' P ' a.txt output all lines, equivalent to cat a.txt
Sed-n ' 4p ' a.txt output line 4th
Sed-n ' 4,7p ' a.txt output line 4th to 7th
Sed-n ' 4,+10p ' a.txt output line 4th and the following 10 lines
Sed-n '/^bin/p ' a.txt output rows beginning with Bin
Sed-n ' $= ' a.txt the number of rows in the output file

[Email protected] ~]# sed-n ' $= '/etc/passwd
47 [No Back directory display]

[Email protected] ~]# wc-l/etc/passwd
47/etc/passwd

"Delete" D Note: Practice without the need to add I, to prevent accidental deletion of files!

Sed ' 3,5d ' a.txt Delete line 3rd to 5th
Sed '/xml/d ' a.txt delete all rows that contain XML
Sed '/xml/!d ' a.txt delete lines that do not contain XML! The symbol represents the inverse
Sed '/^install/d ' a.txt delete line starting with install
Sed ' $d ' a.txt delete the last line of a file
Sed '/^$/d ' a.txt Delete all empty lines

"Replace" s (s/old content/new content/Options)

Vim Test.txt Practice Environment
2017 2011 2018
2017 2017 2024
2017 2017 2017

Sed ' s/2017/xxxx/' test.txt replaces all rows matching the first 2017
Sed ' s/2017/xxxx/g ' test.txt replaces all 2017
Sed ' S/2017/XXXX/2 ' test.txt replaces all rows matching the second 2017
Sed ' s/2017//2 ' test.txt to delete all rows matching the second 2017
Sed ' 2S/2017/XXX/2 ' test.txt the second row matching the second 2017 delete
Sed ' 2~2s/2017/xxx/g ' test.txt replaces 2017 of even rows
Sed-n ' S/2017/x[[email protected] ~]# vim anonftp.shxxx/p ' test.txt print
Sed ' 4,7s/^/#/' a.txt//Comment out line 4th to 7th (beginning with #)
Sed ' s/^ #an/an/' a.txt//dismiss the comment of the line beginning with the #an (remove the # from the beginning)

Ifconfig | Sed-n ' 2s/netm. //p ' | sed ' s/. t//' View IP
172.25.0.11

Sed ' s/\/bin\/bash/\sbin\/sh/'/etc/passwd

Sed ' s#/bin/bash#/sbin/sh/# '/etc/passwd

Swap the first letter with the tail letter
Sed-r ' s/^ (.) (.*) (.) $/\3\2\1/'/wooo.txt
"Each line of text is split into" 1th character "," all characters in the middle "," last 1th character "three parts, and then rearrange the order to" 3-2-1 "by replacing the operation

Dello Worlh
OA GA had
Io xi za za huw

Enlarge all UPPERCASE Brackets
Sed-r ' s/([A-z])/[\1]/'/wooo.txt
Hell[o] World
D[a] Ga Hao
[W]o xi za za hui

Scripting: "Implementing automatic installation of FTP packages and configuring others to access and upload"
Installing the VSFTPD package with Yum
Modify the VSFTPD service configuration to turn on anonymous uploads
Adjust/var/ftp/pub directory permissions to allow FTP write
Start the VSFTPD service and set the boot from run

#!/bin/bash
Yum-y Install VSFTPD
Cp/etc/vsftpd/vsftpd.conf{,.back}
Sed-i ' s/^ #an/an/'/etc/vsftpd/vsftpd.conf
chmod 777/var/ftp/pub
Systemctl Restart VSFTPD
Systemctl Enable VSFTPD

Unable to access common causes
1) firewall is not open
2) SELinux not open
3) Restart Service

sed [option] ' conditional directive ' file .....
Option:-n-i-R
Condition: line number,/regular/
directive: P,d,s,a,i,c

A,append Append, after
I,insert INSERT, Front
C, delete and insert the row

: R Read in
: W Save As
Sed ' 2r/etc/hosts ' tmp.txt reads the file tmp.txt into the second line of the file/etc/hosts
Sed ' w/root/b.txt ' tmp.txt save file Tmp.txt as file Root/b.txt inside
Sed ' 2w/root/b.txt ' tmp.txt will

Script: Filter users and display passwords
#!/bin/bash
A=sed -n /bash$/s/:.*//p /etc/passwd
For I in $a
Do
pass=grep $i /etc/shadow
pass1=${pass#:}
pass2=${pass1%%:
}
echo "$i------> $pass 2"
Done

Use of SED tools and regular expressions (Shell fourth day)

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.