VI Edit string substitution and sed usage

Source: Internet
Author: User

1, vi Edit the string in the replacement file

: S/nice/good/replaces the current line the first nice is good

: S/nice/good/g replaces the current line all nice is good

: N, $s/nice/good/replaces the first nice for each row in the beginning of the nth row to the last row good

: N, $s/nice/good/g replace the nth line to start with the last row in each row all nice is good

N is a number, if N is., indicating the beginning of the current line to the last row

:%s/nice/good/(equivalent to: g/nice/s//good/) replaces the first nice of each line as a good

:%s/nice/good/g (equivalent to: g/nice/s//good/g) replaces all nice in each row as good

You can use # as a delimiter, at which time the middle/not as a delimiter

: s#nice/#good/# Replace the current row the first nice/is good/

:%s#/usr/bin#/bin#g

Replace all the paths in the file with/usr/bin/bin

2. Sed-i bulk replacement of strings in files

Format: Sed-i "s/lookup field/replace field/g" ' grep lookup field-rl path '
Sed-i "s/oldstring/newstring/g" ' grep oldstring-rl yourdir '
For example: Replace the nice in all files under/home/ock as book
Sed-i "s/nice/book/g" ' grep nice-rl/home '
Exp:sed-i "s/shabi/$/g" ' grep shabi-rl./'

Replace the text "garden" in file 1.txt with "Mirgarden"
Sed-i "S/garden/mirgarden/g" 1.txt
Replace "garden" in all files in the current directory with "Mirgarden"
Sed-i "S/garden/mirgarden/g"

VI Edit string substitution and sed usage

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.