sed Introduction to Commands:
Text Editor, stream editor, line editor. The original file is not edited by default, only the data in the pattern space is processed;
After processing is finished, the mode space is printed to the screen;
1 . Command format:
sed [option] ' Addresscommand ' file ... Edit this or a section of these files
Address : (indicates action text range)
1 , Startline,endline
like 1,100 .
$ : Last line
2 ,/regexp/using regular expressions to match patterns must use//
/^root/
3 ,/pattern1/,/pattern2/
The first line that is matched by the pattern1 begins at the end of the line that is first matched to the pattern2, and all the rows in the middle
4 , LineNumber
Specify a row
5 , StartLine, +n
starting from startline, n rows backward;
Command : (Indicates an action command)
D: Delete rows that match the criteria;
P: displays rows that meet the criteria;
a \string: appends a new row to the specified row, with the contents of string
\ n : can be used for line wrapping
I \string: adds a new row before the specified line, with the contents of string
R FILE: adds the contents of the specified file to a qualifying line
W FILE: Saves the row in the range specified by the address to the specified file;
s/pattern/string/ modifier: Find and Replace, default only for the first time in each row is the pattern
The string to match to (the string to which the pattern is being looked for, string to replace)
S/pattern/string/g This means that all the found strings are replaced
Add modifier
g: Global Substitution
I: Ignore character case
s///: s###, [email protected]@@ 分割 can also use # # #和 @@@ 作为 Separators
\ (\), \1, \2
L.. E:like-->liker
Love-->lover
Like-->like
Love-->love sed ' s/l (.. e)/l\1/g '
&: the reference pattern matches the entire string
Sed ' s/\ (L.. e\)/\1r/g ' = sed ' s/l. E/&r/g '
2 . Command function:
Text Editor, stream editor, line editor. The original file is not edited by default, only the data in the pattern space is processed;
After processing is finished, the mode space is printed to the screen;
3 . Command parameters:
- N: silent mode, which does not display the contents of the mode space by default
- I.: Modify the original file directly
- e script-e SCRIPT: multiple scripts can be executed at the same time
-f/path/to/sed_script
Sed-f/path/to/scripts File
Scripts that represent/path/to/scripts are executed on file files.
-r: Indicates the use of extended regular expressions
4 . Command instance:
1, delete the/etc/grub.conf file in the beginning of the blank character;
Sed-r ' s/^[[:space:]]+//g '/etc/grub.conf
[Email protected] ~]# sed-r ' s/^[[:space:]]+//g '/etc/grub.conf
# Note that you don't have the to rerun grub after making changes to the This file
# notice:you has a/boot partition. This means
# all kernel and INITRD paths is relative to/boot/, eg.
# root (hd0,0)
# kernel/vmlinuz-version RO root=/dev/sda2
# initrd/initrd-[generic-]version.img
#boot =/DEV/SDA
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
Sed ' s/\ (id:\) [[:d igit:]]\ (: initdefault:\)/\15\2/g '/etc/inittab
[[Email protected] ~]# sed ' s/\ (id:\) [[:d igit:]]\ (: initdefault:\)/\15\2/g '/etc/inittab
# 0-halt (do not set Initdefault to this)
# 1-single User mode
# 2-multiuser, without NFS (the same as 3, if you don't have networking)
# 3-full Multiuser mode
# 4-unused
# 5-x11
# 6-reboot (do not set Initdefault to this)
#
Id:5:initdefault:
3 , delete the # at the beginning of the/etc/inittab file;
Sed ' s/^#//g '/etc/inittab
[[Email protected] ~]# sed ' s/^#//g '/etc/inittab
0-halt (do not set Initdefault to this)
1-single User mode
2-multiuser, without NFS (the same as 3, if you don't have networking)
3-full multiuser mode
4-unused
5-x11
6-reboot (do not set Initdefault to this)
Id:3:initdefault:
4 , delete blank line in/etc/inittab file;
Sed '/^$/d '/etc/inittab
5 , delete the # number at the beginning of a file and the following white space characters, but require that the # sign must have a blank character after it;
Sed ' s/^#[[:space:]]\{1,\}//g '/etc/inittab
or Sed-r ' s/^#[[:space:]]+//g '/etc/inittab
[[Email protected] ~]# sed ' s/^#[[:space:]]\{1,\}//g '/etc/inittab
Inittab is a used by upstart for the default runlevel.
#
ADDING other CONFIGURATION, here would have the NO EFFECT on YOUR SYSTEM.
#
System initialization is started by/etc/init/rcs.conf
#
Individual runlevels is started by/etc/init/rc.conf
#
Ctrl-alt-delete is handled by/etc/init/control-alt-delete.conf
#
Terminal Gettys is handled by/etc/init/tty.conf and/etc/init/serial.conf,
With configuration In/etc/sysconfig/init.
#
For information on how to write upstart event handlers, or how
Upstart works, see Init (5), init (8), and Initctl (8).
#
Default RunLevel. The runlevels used is:
0-halt (do not set Initdefault to this)
1-single User mode
2-multiuser, without NFS (the same as 3, if you don't have networking)
3-full multiuser mode
4-unused
5-x11
6-reboot (do not set Initdefault to this)
Id:3:initdefault:
6 , delete the white space characters in a file followed by a white-space character and the beginning of the line in the # class, and #
Sed ' s/^[[:space:]]\{1,\}#//g '
7 , take out a directory name for a file path;
echo "/etc/inittab/" | Sed-r ' s#^ (/.*/) [^/]+/?#\1#g '
[Email protected] test]# echo "/etc/inittab/" | Sed-r ' s#^ (/.*/) [^/]+/?#\1#g '
/etc/
Take the file name (base name):
echo "/etc/inittab/" | Sed-r ' s#^/.*/([^/]+)/?#\1#g '
[Email protected] test]# echo "/etc/inittab/" | Sed-r ' s#^/.*/([^/]+)/?#\1#g '
Inittab
This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1864068
Linux commands: SED