A case study on the SED of the Three Musketeers of Linux

Source: Internet
Author: User

Turn http://oldboy.blog.51cto.com/2561410/949365

Origin: After the training class, the students on the SED home Plus to deal with the problem of enthusiasm, the atmosphere is quite good, but there are some students or some dizzy, so, write this article. If you are familiar with this example, most of the common techniques of SED and regular are easy to master.

If there is no, the old boy will take out the last explanation must kill skill (first secret, hey!) Students think that it is impossible, unless you voluntarily give up, haha. Come on, open the whole!

Old boy Linux Training the first exam of the third section of the course, the question is as follows

1. How to Obtain the/etiantian file permissions corresponding to the digital content, such as-rw-r--r--644, requires the use of commands to obtain 644 or 0644 such numbers.

Answer:

The solution of this problem we have introduced no more than 10 kinds of, here to everyone in the simplest way to explain the most difficult to understand the implementation of the SED special usage home Plus.

Because of the difficulty is relatively large, in order to let the students understand more clearly, we need to take a few steps first.

Step 1:sed related command set option parameters

Table 1-1 sed command

Command Function
G When used in conjunction with S, represents a global match substitution for the current row (different from the next G meaning)
P Print matching lines
S Often said to find and replace, with one string replaced by another

Table 1-2 sed options

Options Function
-E Allow multiple edits
-N Cancel default Output

Detailed parameters, command information can refer to "Old Boy Linux Practical Training lesson plan-powerful Stream editor sed detailed guide"

Step 2:sed Replace basic command

Sed-n ' s#▇#▲ #g '~]# cat Oldboy.log▇

Execute the following command to replace the ▇ with ▲, note: The original file has not changed.

[[Email protected] ~]# sed ' s#▇#▲ #g ' oldboy.log▲

Hint: Where delimiter #, you can use/,%,@ and other alternatives, detailed use can refer to old boy's related documents.

Step 3: Basic usage of regular expressions (three Musketeers Grep,sed,awk support regular Expressions)

Special characters Meanings and examples
^word Search for characters (word ) begins with the line.

Example: Grep-v ' ^# ' Oldboy.log

Filters out lines beginning with # (start), which is typically used when viewing a configuration file.

word$ Search for characters (word ) end of the line.

Example: Grep-n '!$ ' oldboy.log

Search for lines ending with a! (exclamation mark)

. (Point number) Representative tangent can only represent “ any one ” character,“ a ” is an arbitrary character

Example: Grep-n ' E.E ' Oldboy.log

The search string can be (Eve), (Eae), (EEE), (e-e), but not only (EE), E and E must have only one character, and the space character is also

* Repeat 0 or more of the previous repeating character

Example: Grep-n ' ess* ' Oldboy.log

Find the string containing (es), (Ess), (ESSS), etc., note that because (*) can be 0, ES is also compliant with the search string. Also, because (*) is a symbol that repeats “ the previous character ”, it must be preceded by a repeating character before (*). Any character is (. *)

.* According to the preceding single character, we know. * Match all strings

Step 4: sed in \( \) and the \1 the function
1) The function of the SED \ (\) remembers part of the regular expression, where \1 is the first remembered pattern, the match in the first parenthesis, \2 the second remembered pattern, that is, the match in the second parenthesis, the SED can remember up to 9.
Example 1:echo I am Oldboy teacher.
If you want to keep a line of words, delete the remainder, and use parentheses to mark the part you want to keep:
In this case we want to keep Oldboy to remove the other content.

[Email protected] ~]# echo I am Oldboy teacher. >~~]# sed ' s#^.*am \ ([a-z].*\) tea.*$#\1#g ' test.txt oldboy

Command description: #命令说明中的-Replace space

A) ^.*am---> this sentence means to start with any character to am-, matching the “i am-” string in the file,

b) \ ([a-z].*\)---> This sentence of the shell is the brackets \ (\), the inside of [a-z] to match any one of 26 letters, [a-z].* together is to match any number of characters, the subject is to match the Oldboy string, Because Oldboy strings need to be preserved, they are enclosed in parentheses, followed by \1 to fetch the Oldboy string.

c)-tea.*$ to the end of any character with a space tea start, which is actually the string “-teacher.&rdquo after matching the Oldboy string;.

D) The \1 in the later replaced content is the contents of the preceding parentheses, which is the Oldboy string we want.

In fact, you went to this level, the topic of this article answer you should be.

Step 5: The technique of taking strings ==> this step of thinking skills is the most important, but also the old boy Linux training courses in a small case.

[[email protected] ~]# stat/ett|sed-n ' 4p ' Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)

1) The string used to handle the desired target (the obtained string, such as the 644 at the beginning of this article) is generally: Start (^.*) to match the beginning of the match before the end of the target is written with the actual characters, such as: “^.* (0” expression match “ Access: (0” Speaking of which, let's give an example:

644/-rw-r--r--) Uid: (0/root) Gid: (0/root)

As you can see, the front part of 644 has been removed.

2) The content after the target is usually written with the actual character at the beginning of the match after the target, and the end is used to ... End (. *$) to match, such as “/-r.*$” expression match “/-rw-r--r--) Uid: (0/root) Gid: (0/root) ” in an example:

[[email protected] ~]# stat/ett|sed-n ' 4p ' |sed ' s#/-r.*$# #g ' Access: (0644

Haha, this time the back of 644 was deleted again.

One thing to note is that the actual character selection is best to be unique, because the regular expression is greedy, and it always matches as far as possible to match the content. Also do not drop the space in the string.

Together is the answer.

[[email protected] ~]# stat/ett|sed-n ' 4p ' |sed ' s#^.* (0# #g ' |sed ' s#/-r.*$# #g ' 644

First delete the target before, after the deletion of the target, this is the application of two sed, each time only half of the match, the first match the target, the second match target, then can directly match the whole line of string it?

After all the steps come up, the subject of this article is then drawn.

Key person Appearances:

With the front steps, now this problem is much simpler, good first throw the answer, and then in the analysis of the explanation.

[[email protected] ~]# stat/ett|sed-n ' s#^.* (0\ ([0-7].*\) \/-.*$#\1#GP '  644

Command Description:

A) “^.* (0” matches the content before the target, this does not have to explain again, here is the match is “ Access: (0

b) “\/-.*$” Match the content after the target, this also no need to explain it, here is the match “/-rw-r--r--) Uid: (0/root) Gid: (0/root) ”

c) The middle of the 644 is what we want to print the results, so we have to enclose in parentheses, so \ (\) no longer explain, [0-7].* is matched to 644, matched to the slash/before, although not exact match but it is enough. Therefore, “\ ([0-7].*\) ” matching is “644”, the front steps have been said, matching the mode \ (\) content, you can use the \ One to take out (if the second expansion number is \2, and so on), so Ah, 644 of the results are pulled out.

PostScript Summary:

Of course, the matching method is not only this one, but also can write a lot of kinds, but the old boy teacher gave me a simple summary of the idea of a beginner's memory, if you have better memory of the method, you can tell me ah.

Students can take the IP address of the network card practiced hand, test, see your knife grinding fast? Ha ha!

Additional solution answers included Summary :

[[email protected] ~]# touch/ett1) [[email protected]~]# stat/ett|sed-n ' 4p ' |awk-f ' [(/] ' {print $} ' 0644 2) [[email protected]~]# stat/ett|sed-n ' 4p ' |cut-d "/"-f1|cut-d "("-F20644 3) [[email protected]~]# stat/ett|head-4|tail-1|cut-d "/"-f1|cut-d "("-F20644 4) [[email protected]~]# stat/ett|head-4|tail-1|awk-f "/" ' {print $} ' |awk-f ' ("' {print $} ' 0644 5) [[email protected]~]# stat-c%a/ett644 6) [[email protected]~]# stat/ett|sed-n ' 4p ' |sed ' s#^.* (0# #g ' |sed ' s#/-.*$# #g ' 6447) [[email protected]~]# stat/ett|sed-n ' s#^.* (0\ ([0-7].*\) \/-.*$#\1#GP ' 6448) [[email protected]~]# stat/ett|sed-n ' 4p ' |sed-ne ' s/^.* (0\ ([^]*\) \/-.*$/\1/gp ' 644 9) [[email protected]~]# stat/ett|sed-n '/access: (/P ' |sed-r ' s/^.*\ (0 (. *) \/-.*$/\1/' 644[[Email protected]~]# stat/ett|sed-n ' 4p ' |sed-e ' s/^access: (\ ([0-9]*\) \/-rw-r.*$/\1/g ' 0644 10) [[email protected]~]# stat/ett|grep "Access: (0" |cut-c 10-13 0644[[Email protected]~]# stat/etc/hosts|sed-nr ' 4s#^.*\ (0 (. *)/-.*$#\1#GP ' 644

Old boy Quotes ⑧⑥-attitude decides everything!

A good attitude is half the success!

A case study on the SED of the Three Musketeers of Linux

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.