How do I replace all of the specific characters in a file? ---linux sed command (Text Edit command) (reprint)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/year_9/article/details/20318407

  

Sed is a very good file processing tool, mainly in the Behavior unit processing, you can replace the data rows, delete, add, select and other specific work. (Total: Sed processing object is a file, how to deal with it?) Is it done on a line-by-row basis? Replace, delete: )
The SED command line format is:

sed [-nefri] ' command ' input text

Common options:

-I: Directly modify the contents of the read file instead of the screen output.

-N: Use Quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.

Common commands:
A: New, a can be followed by a string, and these strings will appear on a new line (the current next line) ~ such as: sed ' 1a end ' after the last line and then insert a line end (interview question)
C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!
D: Delete, because it is deleted ah, so d usually do not pick up any boom;
I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line);
S: Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! For example 1,20s/old/new/g is!

P: Print, that is, print out a selected material. Normally p will work with parameter Sed-n ~

example: (Suppose we have a file named ab)
       Delete a row
     [[email protected] Ruby]# sed ' 1d ' ab              #删除第一行  
     [[email protected] ruby] # sed ' $d ' ab                #删除最后一行
     [email  protected] Ruby] # sed ' 1,2d ' ab             #删除第一行到第二行
     [[email protected] ruby] # sed ' 2, $d ' ab            #删除第二行到最后一行

Show a row
. [email protected] Ruby]# sed-n ' 1p ' ab #显示第一行
[[email protected] ruby] # sed-n ' $p ' ab #显示最后一行
[[email protected] ruby] # sed-n ' 1,2p ' ab #显示第一行到第二行
[[email protected] ruby] # sed-n ' 2, $p ' AB #显示第二行到最后一行


Querying using a pattern
[[email protected] ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[[email protected] ruby] # sed-n '/\$/p ' AB #查询包括关键字 $ where all lines, using backslashes \ Shielding special meaning

Add one or more lines of string
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1a drink tea ' ab#第一行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1,3a drink tea ' ab #第一行到第三行后增加字符串 "Drink Tea"
     hello!
     drink Tea
     Ruby is me,welcome to my blog.
     drink Tea
     end
     drink Tea
     [[email protected] ruby] # sed ' 1a drink tea\nor Coffee ' ab   #第一行后增加多行, use line break \ n
     hello!
     drink Tea
     or coffee
     Ruby is Me , Welcome to my blog.
     End

Instead of one or more lines
     [[email protected] ruby] # sed ' 1c Hi ' ab& nbsp;               #第一行代替为Hi
     Hi
     Ruby is Me, Welcome to my blog.
     End
     [[email protected] Ruby] # sed ' 1,2c Hi ' ab              #第一行到第二行代替为Hi
     Hi
     End

replace part of a row
format: SED "s/the string to replace /new string /g"    (the string to be replaced can be used with regular expressions)
     [[email protected] Ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby/bird/g '     #替换ruby为bird
  [[email protected] ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby//g '         #删除ruby

Insert
[[email protected] ruby] # sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 "Bye"
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
Bye

Summarize:

1, insert a row of data in the last line

2, find the line containing the MO string, and display it (generally with grep, but SED can also)

3. Delete a string (all of the files)

4. Find more specific characters in the file and replace them!

SED handles file objects, line by row!

You can delete a file by deleting the row: sed-i ' XXd ' filename

Delete specific characters: Sed-i ' s/string//g '

Replace Replace line:

To replace a specific character:

"Add" adds a row:


"Choose" sed-n ' xxx/p ' filename

How do I replace all of the specific characters in a file? ---linux sed command (Text Edit command) (reprint)

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.