Linux Three Musketeers Enterprise-class classic face question answer combat

Source: Internet
Author: User

Linux Three Musketeers business-class classic face question answer combat

Description: From the morning old boy education online class one of the students ' face examination questions and the student gives the answer to organize

Interview Question: Please filter oldboy.log in the device: {} how many times Oldboy, filtered and counted out.

Oldboy is a linuxer.

Device: {

Oo

Oldboy

No SQL

This is log

Niu Niu

}

Oldboy

Device: {

Oldboy

No SQL

This is log

Niu Niu

}

Oldboy

Device: {

Oldboy

No SQL

This is log

Niu Niu

}

Device: {

Oldboy

No SQL

This is log

Niu Niu

}

Solution: The use of sed and awk to take the interval:

1) Use the number of rows to take the interval

[email protected] ~]$ seq >test.log

[Email protected] ~]$ sed-n ' 2,5p ' Test.log

2

3

4

5

[Email protected] ~]$ awk ' nr>1&&nr<6 ' Test.log

2

3

4

5

2) using string matching to fetch intervals

The subject can be "device" at the beginning of "}" end, and then the inside of the matching string to filter the count on it.

The method to take the interval:

Sed-n '/^device/,/\}$/p ' Oldboy.log

awk '/device: {/,/}/' Oldboy.log

Tips:

sed-n '/ interval start identification /,/ interval End ID /p ' oldboy.log

3) Complete answer to the question: here are 3 ways

Sed-n '/^device/,/\}$/p ' oldboy.log|grep-w "Oldboy" |wc-l

awk '/device: {/,/}/' oldboy.log|grep-w oldboy|wc-l

awk '/device: {/,/}/{if ($0== "Oldboy") count++}end{print count} ' Oldboy.log

Demonstrate:

[Email protected] ~]$ sed-n '/^device/,/\}$/p ' oldboy.log|grep-w "Oldboy" |wc-l

4

[[email protected] ~]$ awk '/device: {/,/}/{if ($0== "Oldboy") count++} end{print count} ' Oldboy.log

4

[Email protected] ~]$ awk '/device: {/,/}/' oldboy.log|grep-w oldboy|wc-l

4

4) Considering the commonality in the enterprise of the face question, a line may also have multiple Oldboy, that is, the original question should read as follows:

[email protected] ~]$ cat Oldboy.log

Oldboy is a linuxer.

Device: {

Oo

Oldboy Oldboy oldboy

No SQL

This is log

Niu Niu

}

Oldboy

Device: {

Oldboy Oldboy

No SQL

This is log

Niu Niu

}

Oldboy Oldboy

Device: {

Oldboy Oldboy

No SQL

This is log

Niu Niu

}

Device: {

Oldboy Oldboy

No SQL

This is log

Niu Niu

}

Oldboy

So the best answer is:

Sed-n '/^device/,/\}$/p ' oldboy.log|xargs-n 1|sort|grep-w oldboy|wc-l

awk '/device: {/,/}/' oldboy.log|grep-w oldboy|tr "" \ n "|wc-l

Demonstrate:

[Email protected] ~]$ sed-n '/^device/,/\}$/p ' oldboy.log|xargs-n 1|sort|grep-w oldboy|wc-l

9

[Email protected] ~]$ awk '/device: {/,/}/' oldboy.log|grep-w oldboy|tr "" \ n "|wc-l

9

Ordinary people can be on the topic, the expert's answer to consider versatility, master and low hand difference lies in this!

The third book of the old boy's education, "study with the old boy" Linux operations: Proficient in the Three Musketeers of Linux will soon be published, please look forward to!

operation and Maintenance Q Group 384467551 architect Q Group 390642196

Dabigatran (be sure to mark the source) for freeLinux Three Musketeers Practice "ebook set!"

student Movement Good job, choose Beijing old boy IT Education!

The second book of the old boy's education, "study with the old boy" Linux Ops:Shell Advanced Programming will also be published!

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/88/86/wKioL1f7G9qBjzUCAACVlMlnqzc510.jpg "title=" Old boy public QR code. jpg "alt=" wkiol1f7g9qbjzucaacvlmlnqzc510.jpg "/>

Scan the code to pay more attention to the latest articles!


This article is from the "Old Boys Linux Training" blog, make sure to keep this source http://oldboy.blog.51cto.com/2561410/1860164

Linux Three Musketeers Enterprise-class classic face question answer combat

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.