Linux system maintenance process, often through automatic script to deal with operations, and as the most commonly used language is the shell to complete the script. When it comes to substitution we often use SED to do the replacement work.
Here is a brief summary of the functions of SED to facilitate the application of subsequent scripts
The most basic format of SED follows the following form:
sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...
-e-i is commonly used in [OPTION].
-e means that the modified object is displayed on the command line, but the object itself is not actually modified. You can understand that only the modified effect is shown.
-I replaces the modified object directly, and once the modification cannot be rolled back.
I show by example:
[Email protected] logs]# sed-e ' S/kernel.shmmax =. */kernel.shmmax = 123/'/etc/ sysctl.conf after the command executes, the command line displays the Kernel.shmmax as it was modified. # Controls The maximum number of shared memory segments, in PAGESKERNEL.MSGMNB = 6553600kernel.msgmax = 6553600kernel.shmm Ax = 123kernel.shmall = 2097152kernel.shmmni = 4096 while actually viewing the results, stating that the parameter is modified [[email protected] logs]# grep ' Kernel.shmmax '/etc /sysctl.conf Kernel.shmmax = 8589934592 When the-i parameter is used, the corresponding object in the file is modified directly [[email protected] logs]# sed-i ' S/kernel.shmmax =. */ Kernel.shmmax = 123/'/etc/sysctl.conf [[email protected] logs]# grep ' Kernel.shmmax '/etc/sysctl.conf Kernel.shmmax = 123
The above is the most basic use of the SED command, now let's discuss its matching function in the advanced order.
Sed{script-only-if-no-other-script} with matching function, can be a character match, or it can be a line match, the specific usage is as follows
[Email protected] logs]# sed-e '/password/s/include/exclude/g '/etc/pam.d/login #%pam-1.0auth [User_unknown=ignore Success=ok Ignore=ignore Default=bad] Pam_securetty.soauth include System-authaccount required Pam_nolog In.soaccount include System-authpassword exclude System-auth
You can see that the include in the line matching the password is converted to exclude, specifying the matching row by value
[Email protected] logs]# sed-e ' 1,6 s/include/exclude/g '/etc/pam.d/login #%pam-1.0auth [User_unknown=ignore success= OK Ignore=ignore Default=bad] Pam_securetty.soauth exclude System-authaccount required PAM_NOLOGIN.SOACC Ount exclude System-authpassword Exclude System-auth
Someone asked, if I don't want to use substitution, would you like to insert it directly into the specified position?
[Email protected] logs]# sed-e ' 5,6 iusername exclude System-auth '/etc/pam.d/login #%pam-1.0auth [user_unknown=i Gnore Success=ok Ignore=ignore Default=bad] Pam_securetty.soauth include System-authaccount required Pam _nologin.sousername exclude system-authaccount include system-authusername exclude System-authpasswo Rd include System-auth
You can see that the s in quotation marks are changed to I, that is, insert, the statement above indicates that I insert a line after 5 and 6 lines. I'm now going to delete 5, 62 rows of records
1 #%pam-1.02 auth [user_unknown=ignore success=ok ignore=ignore Default=bad] PAM_SECURETTY.SO3 auth include Sys Tem-auth4 account required Pam_nologin.so5 account include System-auth6 password include System-auth [Email protected] logs]# sed-e ' 5,6d '/etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success=ok ignore=ignore DEFA Ult=bad] Pam_securetty.soauth include System-authaccount required pam_nologin.so
Other matching modes:
^ The beginning of the Anchorage line, the end of the $ Anchorage line [[email protected] logs]# sed -e '/^a/ s/include/exclude/g ' /etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success=ok ignore=ignore default= bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.soaccount exclude system-authpassword Include system-auth[[email protected] logs]# sed -e '/h$/ s/include/exclude/g ' /etc/pam.d/login #%PAM-1.0auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.soaccount exclude system-authpassword exclude system-auth
Match single or multiple arbitrary characters
. single non-newline character, * matches any number of characters (I tried to anchor the beginning of the line is not successful), [] matches the character or range of values ([^] is not in the range), \ (char\) Saves the contents of char for subsequent calls to,& instead of replaced content \<,\> anchor the beginning and end of the word, x\{2\},x\{2,\},x\{2,3\}, match x characters 2 times, match x characters at least 2 times, match x characters 2 to 3 times. [[email protected] logs]# sed -e '/^a. H/ s/include/exclude/g ' /etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success= ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.soaccount include system-auth[[email protected] logs]# sed -e '/a*h/ s/include/exclude/g ' / etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success=ok ignore=ignore default= Bad] pam_securetty.soauth exclude &nBsp; system-authaccount required pam_ nologin.soaccount exclude system-authpassword exclude system-auth[[email protected] logs]# sed -e '/^a[a-z][a-z]h/ s/include/exclude/g ' /etc/pam.d/login #%pam-1.0auth [user_ unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.soaccount include system-auth [[email protected] logs]# sed -e '/^[^b-st-z ].. H/ s/include/exclude/g ' /etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success= Ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.soaccount include system-authpassword Include system-auth[[email protected] logs]# sed -e '/^[^b-st-z] ... H/ s/in\ (clude\)/ex\1/g ' /etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success= ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_nologin.so[[email protected] logs]# sed -e '/^[^b-st-z] ... H/ s/clude/ex&/g ' /etc/pam.d/login #%pam-1.0auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.soauth inexclude system-authaccount required pam_ nologin.so[[email protected] logs]# sed -e '/\<a.*h/ s/include/exclude/g ' /etc/pam.d/login #%PAM-1.0auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.soauth exclude system-authaccount required pam_ nologin.so[[email protected] logs]# sed -e '/c\{2,\}/ s/required/unrequired/g ' /etc/pam.d/login #%PAM-1.0auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.soauth include system-authaccount unrequired pam_nologin.so
This article is from the "linuxoracle" blog, make sure to keep this source http://onlinekof2001.blog.51cto.com/3106724/1621080
Use of the SED command for Linux