Shell face question Finishing

Source: Internet
Author: User

1. Give a file with IP , one IP line, to count which IP has the most occurrences.

Ip_input.txt content is as follows:

219.217.49.14

175.43.4.87

87.48.98.1

59.73.38.25

219.217.50.14

59.92.48.32

219.217.49.14

59.72.38.142

59.73.38.25

219.217.49.14

The following are implemented with shell scripts:

Sort Uniq Sort Tail -1awk ' {print $2} '

The command disassembly results are as follows:

SortIp_input.txt:Sortip_input.txt:|Uniq-C175.43.4.87              1 175.43.4.87 219.217.49.14            3 219.217.49.14219.217.49.14            1 219.217.50.14219.217.49.14            1 59.72.38.142219.217.50.14            2 59.73.38.2559.72.38.142             1 59.92.48.3259.73.38.25              1 59.92.48.3259.73.38.25 59.92.48.32 87.48.98.1

Parsing: Sort before uniq-c(to add - C, otherwise it will not show how many times are repeated), because uniq is checking for contiguous rows. The last thing to note is that awk uses single quotation marks, because if you use double quotation marks , you will parse out the second argument to run the program, using single quotation marks as the original awk a usage that represents the second field of a record.

2. Print out the port number and process number of the SSHD service

First, observe netstat-anp|. The result of grep sshd

The command is as follows:root user

grep sed -n ' s/.*:::\ ([0-9]*\). * \ ([0-9]*\) \/sshd/\1 \ 2/P '  

Need to get parse on line 4, take out port and process number 7572

Using The Replace function of the SED command, the format is as follows:

S/pattern/replacemen/flag

where pattern is the text we want to replace, using the matching pattern represented by the regular expression,

Replacement is a string of generic characters to replace.

Flag is the replacement flag , p replaces the 1 rule-compliant string, and the buffer is output to standard output . G is a global match that replaces all strings in the text line that match the rules, but does not output buffers to standard output.

By test instructions, we can extract the first and 7572, and then replace the line 4 , so print section 4 . Only the 7572 and the lines are printed . because sed is one line to read, so the other rows do not output because they do not match.

Sed-n ' s/.*:::\ ([0-9]*\). * \ ([0-9]*\) \/sshd/\1 \2/p '

which- NTo disable the default output,sedThe default is to output all text, the main next regular expression, here in::: Front plus.*is to put the frontTCP 0 0to include in the comingpatternwithin this regular expression, and then in the\ ([0-9]*\)Add a space to the back and add.*, in order to put the whole Abe used as a whole behind/1to reference, if not.*, the following references/1will become empty. So here it is with.*, the previous character is added, and the previous character here is a blank space. \(Here's\is to escape,

The back of \1 \2 here to represent the 7572

3. Number of occurrences of a word in the statistics text

File for ip_input.txt, this file is on the basis of the previous ip_input.txt added some letters, in order to mix capital letters, plus symbols, so deliberately add the ground, character ' 4' There are five

219.217.49.14

175.43.4.87

87.48.98.1

59.73.38.25

219.217.50.14

59.92.48.32

219.217.49.14 DF,SDF4SD,SDFAD4SDF

59.72.38.142

59.73.38.25 DSAF4ASDF as4e

219.217.49.14 219.217.49.14219.217.49.14 219.217.49.14

The command is as follows:

Cat Ip_input.txt | Tr-c "[: alnum:]" \ n "| Grep-c 4

Ideas are as follows:

Because grep 's- c option only counts the number of rows that a word appears, you must put each word in one line, which requires the replacement command tr, which uses TR use characters other than letters and numbers. \ n , which is replaced by a newline character.

The TR command syntax is as follows:

TR [option] set1 [Set2]

Option

-c set2 replace character Set set1 contains no characters. Here set1 we use the Span style= "font-family: ' Times New Roman ';" >[:alnum:] [ Span style= "font-family: the song Body;" >: alnum : Span style= "font-family: ' Times New Roman ';" >] denotes all alphabetic characters and numbers. &NBSP, set2 is Span style= "font-family: ' Times New Roman ';" >\n

-D: Removes repeated characters from thecharacter set Set1 and does not perform an action.

-S: compress the repeated characters in the Set1 to remove the weight.

-T: Converts the character set Set1 with set2 .

Extension: If the number of times a statistic character appears, you need to change

More TT.TXT|TR-SC [: alnum:] ' \ n ' |sed ' s/value/value\n/g ' |grep-c value

Note: If you want to encapsulate it as a . SH file, such as my.sh , you need to

More $2|TR-SC [: alnum:] ' \ n ' |sed ' s/$1/$1\n/g ' |grep-c $,

Direct./mysh value tt.txt with a result of 7

Note: Double quotes are used in sed, and if you use single quotes, $ is not value because the rules of the SED command are used in single quotes, not the rules of the shell.

[Email protected] my]# more Tt.txt
Valuevaluevalue
Value
valueasf
Value

Shell face question Finishing

Related Article

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.