SED command use

Source: Internet
Author: User

Sed Stream Editor

Sed is a file editing tool that loads all the files into the memory buffer, reads them by line breaks, reads one line from the buffer into its own internal space at a time, finishes editing, and outputs the results to the screen, and the default SED does not make changes to the original file.


SED use format:

sed [options] ' addr1[,addr2] Edit command ' file ... sed [options] ' addr1[,addr2 ' edit command ' file ... \ \ \ \ \ When there is a variable substitution, use double quotation marks

Common options:
-N: Silent mode, does not output the contents of the mode space;
-R: Extended Regular expression
-f/path/to/sed_script_file: Specify sed script file
-E ' script '-e ' script ': Specify multiple editing instructions
-I: Edit the original file directly


Edit command:
 d: Delete
 p: Print
Sed-n '/^#/,/^\//p '/etc/fstab \ \ If you don't use option n will output the contents of the pattern space as well.
 I \text:
# sed '/^#/i \begin ' test.txt \ \ Insert a line before each # line begin
a \text:Behind
R/path/to/somefile:Inserts the contents of another file in the specified location
W/path/to/somefile: Saves all eligible rows to the specified file
=: Displays the line number of the line that matches the criteria

Sed-n '/^\//= '/etc/fstab

Y: Character conversion

Sed '/^#/y/abcdefg/abcdefg/' test.txt

s///: The Find condition can use the pattern, but the content to replace does not


Address delimitation:

Number: Specifies the unique row for the input file.

Startline,endline: Specifies the start and end lines, such as: 1,5, from 1 lines to the end of line fifth.

/pat1/,/pat2/: The first line to which the pattern is matched, and the last one to be matched by the pattern

# sed-n '/^#/,/[[:d igit:]]/p ' test.txt

/pattern/: The line to which the pattern is matched, with the regular expression in the middle, and the last regular expression if the middle is empty.

Addr1,+n: Matches the contents of address 1 and the following N rows, such as 3,+4, that represent the beginning of 3 lines and 4 rows from the following.

# sed ' 3,+4d ' test.txt

An extended regular expression is matched between \cpatternc:\c and C, and the C character can be any character.


Some instructions are detailed:

Find replacement S, in the following format

# sed ' [address]s/pattern/replacement/flags '

Address is bound, ellipsis represents full-text search.

s to replace the specified

Pattern for matching patterns

Replacement for content that needs to be replaced

Flags as tags, g for global substitution, p for content in print mode space, i:ignore-case, character case insensitive


Example:

If a specified is too long, then it is necessary to use the-F option to specify the SED script file, in the script file can contain multiple lines of instructions to modify the file

Original file:

Device=eth0
Type=ethernet
Onboot=no
Nm_controlled=yes
Bootproto=none
netmask=255.255.255.0
gateway=192.168.0.1
dns1=114.114.114.114

Script

/.*/{
/ONBOOT/C onboot=yes \ \ Replace
/netmask/i ipaddr=192.168.0.29 \ \ Insert
/gateway/y/abcdefghijklmnopqrstuvwxyz/abcdefghijklmnopqrstuvwxyz/\ Conversion
}

Execution results

# sed-f sed.sh Ifcfg-eth0 device=eth0type=ethernetonboot=yesnm_controlled=yesbootproto=noneipaddr= 192.168.0.29netmask=255.255.255.0gateway=192.168.0.1dns1=114.114.114.114




2. Match the line starting with the UUID and read the contents of the/etc/issue to the matching content
# sed '/^uuid/r/etc/issue '/etc/fstab


3. Save the matching line to/tmp/filetable.txt, which is added by the overlay

# sed '/^\//w/tmp/filetable.txt '/etc/fstab


4, have the following content file

He like his liker.
He like his lover.
She Love her liker.
She Love her lover.
(1), delete the above content contains the word "L." E "consistency of line;

# sed '/\ (L.. e\). *\1/d ' Test

(2), in the file "L." E "a consistent line, the last L. The l of the first word of e is changed to uppercase L;

# sed ' s/\ (\ (l\ (. e\) \). *\) \2/\1l\3/g ' test



Practice:
1. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
# sed ' [email protected]\ (id:\) [0-9]\ (: initdefault\) @\15\[email protected] '/etc/inittab
2, delete the blank line in the/etc/init.d/funcions file;
# sed '/^$/d '/etc/ini.t/functions
3. Delete the # at the beginning of the/etc/inittab file;
# sed ' [email protected]^#@@g '/etc/initta
4. Delete # and white space characters at the beginning of the line beginning with # followed by at least one whitespace character in the/etc/rc.d/rc.sysinit file;
# sed ' [email protected]^#[[:space:]]\{1,\}@@g '/etc/rc.d/rc.sysinit
5. Delete the blank characters from the beginning of the/boot/grub/grub.conf file;
# sed ' [email protected]^[[:space:]]\{1,\}@@ '/boot/grub/grub.conf
6, take out a file path directory name, such as/etc/sysconfig/network, its directory is/etc/sysconfig, functions similar to dirname command;
# echo/etc/sysconfig/network-scripts/ifcfg-eth0/| Sed ' [Email protected][^/]\{1,\}/*[email protected]@ '
Meaning to find the last string to begin with, followed by optional/replace with nothing

This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1757409

SED command use

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.