A concise tutorial on SED

Source: Internet
Author: User
Tags processing text

Awk was born in 1977, the 36-year-old SpongeBob, sed 2-3 years older than awk, awk is like Sister Lin, Sed is the brother Bao. So Lin sister jumped a topless, his brother Sed can't sit, also must come out shake a shake.

Sed all called stream editor, stream editors, the way to edit the text in a program, quite hacker AH. SED is basically playing regular pattern matching, so the people who play sed, regular expressions are generally stronger.

Similarly, this article does not say everything about sed, you can refer to the Handbook of SED, I mainly want to compete with you for those from the phone fingers or toilet flow away time, use these time to learn something. Of course, the next step is to rely on the hands of the people themselves.

Replace with s command

I use the following text to do the demo:

$ cat Pets.txtthis is my cat  my cat's name is Bettythis is my dog  my dog's name is Frankthis is my fish  my fis H ' s name is georgethis is my goat  my goat ' s name is Adam

Replace the My string with Hao Chen's, the following statement should be very well understood (s means the replacement command,/my/means match My,/hao Chen ' s/means to replace the match with Hao Chen ' s,/g represents a line replace all matches):

$ sed "S/my/hao Chen ' s/g" Pets.txtthis is Hao Chen's cat  Hao Chen's cat ' s name is Bettythis was Hao Chen's dog  Hao Chen's dog's name is Frankthis is Hao Chen's fish  Hao Chen's Fish ' s name is Georgethis is Hao Chen's goat  Hao Chen ' s goat ' name is Adam

Note: If you want to use single quotation marks, then you can not escape through \ ' This, there are double quotes on it, in double quotes can be escaped with \ ".

Again note: The above SED does not change the contents of the file, just the output of the processed content, if you want to write back the file, you can use redirection, such as:

$ sed "S/my/hao Chen ' s/g" Pets.txt > Hao_pets.txt

Or directly modify the contents of the file using the-I parameter:

$ sed-i "S/my/hao Chen ' s/g" pets.txt

Add something to the front of each line:

$ sed ' s/^/#/g ' pets.txt#this is my cat#  my cat's name is Betty#this is my dog#  my dog's name is Frank#this is my fish#  My fish's name is George#this was my goat#  my goat ' s name is Adam

Add something to the last side of each line:

$ sed ' s/$/---/g ' pets.txtthis is my cat---  my cat's name is Betty---this is my dog---  my dog's name is Frank ---this is my fish---  my fish's name is George---this is my goat---  my goat's name is Adam---

Let's take a look at some of the most basic things about regular expressions:

    • ^ Indicates the beginning of a line. such as:/^#/with # to start the match.
    • $ represents the end of a line. such as:/}$/to the end of the match.
    • \< represents the beginning of the word. such as \<ABC, the word ABC-led.
    • The \> represents the ending. such as abc\>, the word with ABC end.
    • . Represents any single character.
    • * Indicates that a character has occurred 0 or more times.
    • [] The character set is combined. For example: [ABC] means matching A or B or C, and [a-za-z] means matching all 26 characters. If there is a ^ expression counter, such as [^a] represents a character other than a

Positive regular expressions are some of the most important things, such as removing tags from an HTML:

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

Look at our sed command.

# If you do that, there will be a problem $ sed ' s/<.*>//g ' html.txt understand?# to solve the problem above, you have to look like this. # One of the ' [^>] ' specifies that characters except > are repeated 0 or more times. $ sed ' s/<[^>]*>//g ' html.txtthis is what I meant. Understand?

Let's take a look at specifying what to replace:

$ sed "3s/my/your/g" Pets.txtthis is my cat  my cat's name is Bettythis is your dog  my dog's name is Frankthis is M Y fish  My fish ' s name is georgethis is my goat  my goat's name is Adam

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

$ sed "3,6s/my/your/g" Pets.txtthis is my cat  my cat's name is Bettythis was your dog  your dog ' s name is Frankthis Is your fish  your fish's name is georgethis are my goat  my goat ' s name is Adam

$ cat 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 first s of each line:

$ 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 are my goat, my goat ' s name is Adam

Replace only 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 are my goat, my goat ' s name is Adam

Replace only the 3rd after s of 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 FiS H ' s name is georgethis is my goat, my goat ' s name is Adam
Multiple matches

If we need to replace more than one pattern at a time, see the example below: (the first mode replaces my first line to the third line with my your, and the second one replaces the 3rd line with that).

$ sed ' 1,3s/my/your/g; 3, $s/this/that/g ' my.txtthis is your cat, your cat's name is Bettythis was your dog, your dog ' s name is Frankthat is your f Ish, 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 a variable to be matched, and then we can add something in the basic left and right. As shown below:

$ 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 are [my] goat, [my] goat ' s name is Adam
Parentheses match

Example of using parentheses: (a string that matches a regular expression enclosed in parentheses can be used as a variable, using \1,\2 in sed ...) )

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

The regular expression in the above example is a bit more complex, and the following is undone (remove the escape character):

Regular: This is my ([^,]*),. *is (. *)
Matches to: This is my (cat),.......... is (Betty)

Then: \1 is Cat,\2 is Betty

command of the SED

Let's go back to the first example Pets.txt, let's look at a few commands:

n command

First look at the n command--the next line of content into a buffer to match.

The following example puts the even lines in the original text into an odd line match, and s only matches and replaces them once, so it becomes the following result:

$ sed ' n;s/my/your/' pets.txtthis is your cat  my cat's name is Bettythis is your dog  my dog's name is Frankthis is Your fish  My fish's name is georgethis is your goat  my goat ' s name is Adam

In other words, the original file became:

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 are my goat\n  my goat ' s name is Adam

So, here's the example you'll see,

$ 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 f Ish,  My fish's name is georgethis are my goat,  my goat ' s name is Adam
A command and I command

The A command is append, and the I command is insert, which is used to add rows. Such as:

# where 1i indicates that it wants to insert a line before line 1th (insert) $ sed "1 I This is my monkey, my monkey's name is Wukong" My.txtthis 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 Nam E is georgethis is my goat, my goat's name is adam# where 1a indicates that it wants to append a line after the last line (append) $ sed "$ A 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 are my dog, M Y 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:

# Note One of the/fish/a, which means match to/fish/after appending a line of $ sed "/fish/a This is my monkey, my monkey ' s name is Wukong" My.txtthis is my cat, my C At ' s name are Bettythis is my dog, my dog's name is Frankthis is my fish, my fish's name is Georgethis is my monkey, my mon Key ' s name is wukongthis are my goat, my goat ' s name is Adam

The following example is a very good insert for each line:

$ sed "/my/a----" My.txtthis is my cat, my cat's name is Betty----This is my dog, my dog's name is Frank----the Is my fi SH, my fish ' s name is George----The Is my goat, my goat ' s name is Adam----
C command

The C command is to replace the matching row

$ 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, M Y 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 Th Is is my monkey, my monkey's name is Wukong "My.txtthis is my cat, my cat's name is Bettythis are my dog, my dog's name is Frankthis is my monkey, my monkey's name is wukongthis are my goat, my goat ' s name is Adam
d command

Delete a matching row

$ 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 are 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 take this command as a grep command.

# match fish and output, and you can see that fish's line was hit two times, # This is because the SED processing will process the information output $ sed '/fish/p ' 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 fish, my fish's name is georgethis is my go At, my goat's name is adam# use the n parameter just fine $ Sed-n '/fish/p ' my.txtthis is my fish, my fish's name is george# from one mode to another $ sed- N '/dog/,/fish/p ' my.txtthis is my dog, my dog's name is Frankthis is my fish, my fish's name is george# from the first line to the one that matches fish success Line $ Sed-n ' 1,/fish/p ' my.txtthis is my cat, my cat's name is Bettythis is my dog, my dog's name is Frankthis is my fish, m Y Fish ' s name is George
A few points of knowledge

Well, here's a basic point of understanding four sed:

Pattern Space

The 0th one is about the-n parameter, we may not understand, it's okay, let's take a look at the pseudo-code of the SED processing text and learn about the concept of pattern space:

foreach line in file {    //Put the row pattern_space    pattern_space <= lines;    Perform SED commands on each Pattern Space    pattern_space <= EXEC (Sed_cmd, pattern_space);    Output processed pattern_space if    (sed option hasn ' t "-n")  {       print pattern_space    }} If no-n is specified
Address

The first one is about address, almost all of the above commands are like this (note: one of them!) indicates whether a command is executed after a successful match

[Address[,address]] [!] {cmd}

Address can be a number, or it can be a pattern, you can use a comma to separate two address represents two address of the interval, to execute command cmd, pseudo-code as follows:

BOOL Bexec = Falseforeach line in file {    if (match (Address1)) {        bexec = true;    }    if (bexec = = True) {        EXEC (sed_cmd);    }    if (Match (Address2)) {        bexec = false;    }}

About address you can use relative locations, such as:

# where +3 represents a continuous 3 lines after $ sed '/dog/,+3s/^/#/g ' pets.txtthis is my cat  my cat's name is betty# this is my dog#   my dog's NA I am frank# this is my fish#   my fish's name is georgethis are my goat  my goat ' s name is Adam
Command packaging

The second is that CMD can be multiple, they can be separated by semicolons, and can be enclosed in curly braces as nested commands. Here are a few examples:

$ cat Pets.txtthis is my cat  my cat's name is Bettythis is my dog  my dog's name is Frankthis is my fish  my fis H ' s name is georgethis is my goat  my goat's name is adam# on lines 3 to 6th, execute command/this/d$ sed ' 3,6 {/this/d} ' pets.txtthis is my Cat  My cat's name is Betty  my dog's name is Frank  My fish's name is georgethis is my goat  my goat ' s name i S adam# on line 3 to line 6th, match/this/succeeds, then matches/fish/, succeeds after executing d command $ sed ' 3,6 {/this/{/fish/d}} ' Pets.txtthis is my cat  my cat's name is being Ttythis is my dog  my dog's name is Frank  My fish's name is georgethis is my goat  
Hold Space

Third, let's take a look at hold Space.

Next, we need to understand the concept of hold space, let's take a look at four commands:

G: Copy contents of hold space to pattern space, the contents of the original pattern space are cleared
G: After append the contents of hold space to the pattern space\n
H: Copy the contents of the pattern space into hold space, and the contents of the original keep space are erased.
H: Append the contents of the pattern space to the hold space\n
X: Exchanging the contents of pattern space and hold space

What is the use of these commands? Let's take a look at two examples, and the sample files used are:

$ cat T.txtonetwothree

A first example:

$ sed ' h;g ' t.txtoneonetwoonetwothree

is not a bit not understand, I make a diagram you can understand.

The second example, reverse the line of a file:

$ sed ' 1! G;h;$!d ' T.txtthreetwoone

One of ' 1! G;h;$!d ' can be disassembled as three commands

    • 1! g--only the first line does not execute the G command, append the contents of hold space back to the pattern space
    • h--the first line executes the H command to copy the contents of the pattern space into hold space
    • $!d--except the last line does not execute the D command, the other rows execute the d command, delete the forward

This execution sequence is hard to understand, so let's make a diagram as follows:

Let's say so much first, I hope it will be useful to everyone.

(End of full text)

A concise tutorial on 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.