The customary use of SED

Source: Internet
Author: User

1: Comment out a line
This is often encountered, the configuration file in a line to comment out. Let him not work.
sed-i-E ' 121 s/^/#/'/usr/local/apache2/conf/httpd.conf
the meaning of the above line of command is to comment out the 121 lines.
sed-i-E ' 121 s/^/#/'-i-e ' 122 s/^/#/'/usr/local/apache2/conf/httpd.conf
This is to comment out 121,122 lines.

2: Remove the note # to make this line work
Sed-i ' s/#ServerNamewww.example.com:80/ServerNameWww.example.com:80/g '/usr/local/apache2/conf/httpd.conf
The order is to get rid of this line of #. is actually a replacement process.

3: Insert a row at the position of a line

If you just append a row to a document, it's simple. >> can take care of it. However, if it is inserted in a specified line.
Sed-i "8 S/^/alias vi= ' vim '/"/ROOT/.BASHRC
is to insert alias vi= ' vim ' in line 8th

4: How to insert a line, including special characters/
Sed-i "S/^/addtype application\/x-httpd-php. php. phtml/" httpd.conf
This lets Apache support PHP, much simpler.
The following usage has not been tested. Another test.

Sed:
1. Delete the beginning of the line space
Sed ' s/^[]*//g ' filename
Sed ' s/^ *//g ' filename
Sed ' s/^[[:space:]]*//g ' filename

2. Add new lines after line and before line
After line: sed ' s/pattern/&\n/g ' filename
Before line: sed ' s/pattern/\n&/g ' filename
& Representative Pattern

3. Use variable substitution (using double quotes)
Sed-e "s/$var 1/$var 2/g" filename

4. Insert text before the first line

Sed-i ' 1 i\ insert string ' filename

5. Insert in last line
Sed-i ' $ a\ insert string ' filename

6. Insert before matching line
Sed-i '/pattern/i ' Insert string ' ' filename '

7. Insert after matching line
Sed-i '/pattern/a "Insert string" ' Filenam

The customary use of SED

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.