Download from linux and linux

Source: Internet
Author: User

Download from linux and linux

Sed's full name is stream editor, which is a stream editor. It uses a program to edit text, which is equivalent to hacker. Sed is basically playing regular expression matching. Therefore, regular expressions are generally strong for sed users.

Replace with s command

Use the following text for Demonstration:

$ cat pets.txtThis is my catmy cat's name is bettyThis is my dogmy dog's name is frankThis is my fishmy fish's name is georgeThis is my goatmy goat's name is adam

Replace the my string with the Hao Chen's, and the following statement should be well understood (s indicates the replacement command,/my/indicates matching my, /Hao Chen's/indicates replacing the match with Hao Chen's,/g indicates replacing all the matches on a row ):

Replace the my string with the Hao Chen's, and the following statement should be well understood (s indicates the replacement command,/my/indicates matching my, /Hao Chen's/indicates replacing the match with Hao Chen's,/g indicates replacing all the matches on a row ):

Note: If you want to use single quotes, you cannot escape them by \ '. You can use double quotes to escape them. \ "can be used to escape them.

Note: The above sed does not change the file content, but only outputs the processed content. If you want to write back the file, you can use redirection, such:

$ sed"s/my/Hao Chen's/g"pets.txt > hao_pets.txt

Or use the-I parameter to directly modify the file content:

$ sed -i "s/my/Hao Chen's/g" pets.txt
Some basic things of Regular Expressions
  • ^ Indicates the beginning of a row. For example,/^ #/matches a string starting.
  • $ Indicates the end of a row. For example:/} $/matches with the end.
  • \ <Indicates the beginning of the word. For example, \ <abc indicates the word "abc.
  • \> Indicates the end of the word. For example, abc \> indicates the word ending with abc.
  • . Represents any single character.
  • * Indicates that a character appears 0 or multiple times.
  • [] Character Set combination. For example, [abc] indicates matching a, B, or c, and [a-zA-Z] indicates matching all 26 characters. If there is a ^ character in it, it indicates the inverse. For example, [^ a] indicates a character other than.

Remove tags from an html file:

Html.txt

< code>b<This</>b< is what < code>spanstyle="text-decoration: underline;"<I</>span< meant. Understand?

Let's look at our sed command.

# If you do this, there will be problems.

$ sed 's/< >//g' html.txtUnderstand?

# To solve the problem above, you have to do as follows.

# The'[^<]' Besides<Duplicate characters0.

$ sed 's/<[^>]*>//g' html.txtThis is what I meant. Understand?

Let's take a look at the content to be replaced:

$ sed"3s/my/your/g"pets.txtThis is my catmy cat's name is bettyThis is your dogmy dog's name is frankThis is my fishmy fish's name is georgeThis is my goatmy goat's name is adam

The following command only replaces 3rd to 6th lines of text.

$ sed "3,6s/my/your/g"pets.txtThis is my catmy cat's name is bettyThis is your dogyour dog's name is frankThis is your fishyour fish's name is georgeThis is my goatmy goat's name is adam

Only Replace the first s of each row:

$ sed 's/s/S/1' my.txtThiS is my cat, my cat's name is bettyThiS is my dog, my dog's name is frankThiS is my fish, my fish's name is georgeThiS is my goat, my goat's name is adam

Replace the second s of each row:

$ sed's/s/S/2'my.txtThis iS my cat, my cat's name is bettyThis iS my dog, my dog's name is frankThis iS my fish, my fish's name is georgeThis iS my goat, my goat's name is adam

Replace only the second s after the first row:

$ sed's/s/S/3g'my.txtThis is my cat, my cat'S name iS bettyThis is my dog, my dog'S name iS frankThis is my fiSh, my fiSh'S name iS georgeThis is my goat, my goat'S name iS adam
Multiple matches

If we need to replace multiple modes at a time, refer to the example below: (the first mode replaces my from the first row to the third row with your, in the second example, replace This after the first row with That)

$ sed '1,3s/my/your/g; 3,$s/This/That/g'my.txtThis is your cat, your cat's name is bettyThis is your dog, your dog's name is frankThat is your fish, your fish's name is georgeThat is my goat, my goat's name is adam

The above command is equivalent to: (Note: The following uses the-e command line parameter of sed)

Sed -e  '1,3s/my/your/g' -e  '3,$s/This/That/g' my.txt

We can use & as the matched variable, and then add something to the left and right sides. As follows:

$ sed 's/my/[&]/g' my.txtThis is [my] cat, [my] cat's name is bettyThis is [my] dog, [my] dog's name is frankThis is [my] fish, [my] fish's name is georgeThis is [my] goat, [my] goat's name is adam
Parentheses matching

Example of using parentheses for matching: (the string matched by the regular expression enclosed in parentheses can be used as a variable, and \ 1, \ 2… is used in sed ...)

$ sed 's/This is my \([^,]*\),.*is \(.*\)/\1:\2/g' my.txtcat:bettydog:frankfish:georgegoat:adam

In the above example, the regular expression is a bit complex. The following code is used to unbind the escape character ):

The regular expression is: This is my ([^,] *),. * is (.*)
Match: This is my (cat ),.......... Is (betty)

Then: \ 1 is cat, \ 2 is betty

Sed command

Go back to the starting example pets.txt. Let's look at several commands:

$ cat pets.txtThis is my catmy cat's name is bettyThis is my dogmy dog's name is frankThis is my fishmy fish's name is georgeThis is my goatmy goat's name is adam
N command

Let's take a look at the N command First-include the content of the next line as a buffer for matching.

In the following example, the even rows in the original text are included in the odd row match, and s is only matched and replaced once. Therefore, the result is as follows:

$ sed'N;s/my/your/'pets.txtThis is your catmy cat's name is bettyThis is your dogmy dog's name is frankThis is your fishmy fish's name is georgeThis is your goatmy goat's name is adam

That is to say, the original file is:

This is my cat\n  my cat's name is bettyThis is my dog\n  my dog's name is frankThis is my fish\n  my fish's name is georgeThis is my goat\n  my goat's name is adam

In this way, you will understand the following example,

$ sed'N;s/\n/,/'pets.txtThis is my cat,  my cat's name is bettyThis is my dog,  my dog's name is frankThis is my fish,  my fish's name is georgeThis is my goat,  my goat's name is adam
Commands a and I

The a command is append, the I command is insert, and they are used to Add rows. For example:

# 1i indicates that it needs to insert a row before row 1st (insert) $ sed "1 I This is my monkey, my monkey's name is wukong" my.txt This is my monkey, my monkey's name is wukongThis is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam # 1a indicates that it needs to append a row (append) after the last row) $ sed "$ a This is my monkey, my monkey's name is wukong" my.txt This is my cat, my cat's name is bettyThis is my monkey, my monkey's name is wukongThis is my dog, my dog's name is frankThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam

We can use matching to add text:

# Pay Attention to/fish/a, which means adding a row $ sed after matching/fish/"/fish/a This is my monkey, my monkey's name is wukong "my.txt This is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my fish, my fish's name is georgeThis is my monkey, my monkey's name is wukongThis is my goat, my goat's name is adam
C command

C command is to replace Matching lines

$ sed "2 c This is my monkey, my monkey's name is wukong" my.txtThis is my cat, my cat's name is bettyThis is my monkey, my monkey's name is wukongThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam$ sed"/fish/c This is my monkey, my monkey's name is wukong"my.txtThis is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my monkey, my monkey's name is wukongThis is my goat, my goat's name is adam

 

D command

Delete matched rows

$ sed'/fish/d'my.txtThis is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my goat, my goat's name is adam$ sed'2d'my.txtThis is my cat, my cat's name is bettyThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam$ sed'2,$d'my.txtThis is my cat, my cat's name is betty

 

P command

Print command

You can use this command as a grep command.

# Match the fish and output it. We can see that the line of fish is played twice, # This is because sed outputs $ sed '/fish/p'my.txt This is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my fish, my fish's name is georgeThis is my fish, my fish's name is georgeThis is my goat, my goat's name is adam # Just use the n parameter $ sed-n'/fish/p'my.txt This is my fish, my fish's name is george # from one mode to another mode $ sed-n'/dog/,/fish/p'my.txt This is my dog, my dog's name is frankThis is my fish, my fish's name is george # print from the first line to the line that matches the fish success $ sed-n' 1, /fish/p'my.txt This is my cat, my cat's name is bettyThis is my dog, my dog's name is frankThis is my fish, my fish's name is george

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.