The advanced application of the shell script awk

Source: Internet
Author: User

Demand:

Text in the first column of the word typeface, and so on, the second column is added, the final output characters and the final and (for the convenience of final check, gave 1)

The text is as follows:

Cat test.logabc 1aaa 1bbb 1ddd 1sss 1iii 1abc 1sss 1ddd 1ddd 1ddd 1ddd 1bbb 1bbb 1bbb 1bbb 1bbb 1

Idea 1:awk take the first column out, then sort it, go to the Uniq, assign it to the variable x, then iterate through X, grep from the text, and then awk intercepts the second column and add it, which requires multiple awk, multiple read files, slower efficiency, thinking about whether one can read it, and then thinking 2


Ideas 2:cat text After sort, sorted by the first column, so that the equal to appear consecutively, unequal when the description is the next, so awk read, to determine whether the first column is equal to the last, equal plus; print total; note the first and last lines


Idea 2 Code:

#!/bin/bashcat test.log|sort|awk  ' begin{       #排序文本, and read into tmp =   "" total = 0     }{if  (tmp==$1)            #当相等时相加 {        total += $2}else{                            #不相等时, prints the last added value         if   (tmp!= "") {          #处理首行, when the first line is not last, so you need to exclude          print tmp  " total is : "  total  #打印         }        tmp =  $1           #给变量赋新值          total = $2}}end{     #处理末行, the end keyword indicates that the document has been read,         # Reason: Last time the print was last, last time no chance to print         print $1  " total  is :  " total} ' # #备注: This is the interviewer out of a question, I want to use the idea of one, the result he said in awk internal realization, although I have the idea but the grammar does not remember, #而且用手写, the result is miserable


Idea 1 Code:

#!/bin/bashtmp= ' cat Test.log |awk ' {print $ ' |sort|uniq ' #去重后的值sum =0for i in ' echo $tmp ' #遍历去重后的值do num= ' grep $i Test.log|awk ' {print $} ' #过滤出第一列相等的所有值 for J in $num #遍历所有值 do sum=$ (($j + $sum) #相加 doneecho $i "Total is:" $sum #打印sum =0 #重新初始化done

Results:

[[Email protected] ~]# sh a.sh AAA total IS:1ABC Total IS:2BBB Total is:6ddd Total IS:5III total is:1sss Total I S:1[[email protected] ~]# sh b.sh AAA total IS:1ABC Total IS:2BBB Total is:6ddd total IS:5III total is:1sss to Tal Is:1

Summary: The code volume is similar, but the obvious idea 2 advanced. Programming within AWK, not the same syntax, be careful.

1, the definition of the volume, use

2. Braces distinguish code blocks



Python implementations:

#!/usr/bin/env Python2con = open ("Test.log", "r") f1 = []F2 = []F3 = []sum = 0for lines in Con.readlines (): line = lines.                   Strip (). Split ("") F1.append (Line[0]) f3.append (line) F2 = List (set (F1)) #利用集合的特性去重for N in F2:for m in F3: if n = = m[0]: sum + = Int (m[1]) print n + "Total is:" + str (sum) sum = 0con.close ()


The advanced application of the shell script awk

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.