Old boy Education daily-day 126th-print multiplication tables with shell scripts

Source: Internet
Author: User

Old boy Education daily-day 126th-print multiplication tables with shell scripts

Issue background: Generate 9*9 multiplication table
[[email protected] ~]# SEQ 9 | Sed ' h;g ' | Awk-v rs= ' {for (i=1;i<=nf;i++) printf ("%dx%d=%d%s", I, NR, I*nr, I==nr? ") \ n ":" \ T ")} ' 1x1=11x2=2 2x2=41x3=3 2x3=6 3x3=91x4=4 2x4=8 3x4=12 4x4=161x5=5 2x5=10 3x5=15 4x5=20 5x5=251x6= 6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=361x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=491x8=8 2x8=16 3x8=24 4x 8=32 5x8=40 6x8=48 7x8=56 8x8=641x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
Problem analysis Solution First step: Seq 9
[[email protected] ~]# seq 9123456789
Why does the summary use SEQ?
' seq ' Prints the numbers from first to last by INCREMENT. By default, each number is printed on a separate line.

It is easy to see that the number generated by the SEQ is a single line.
If you use echo {1..9}, it will be on one line. The results cannot be transmitted to SED processing.

Step two: sed ' h;g '
[[email protected] ~]# SEQ 9 | Sed ' h;g ' 112123123412345123456123456712345678123456789
Summary:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/05/9D/wKiom1moDvWS22btAAqmEuPRZy0977.png "style=" float : none; "title=" 126-sedz execution process 01.png "alt=" Wkiom1modvws22btaaqmeuprzy0977.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A4/4E/wKioL1moDuPSbL6OAAeVnzNk3uQ953.png "style=" float : none; "title=" 126-sedz execution process 02.png "alt=" Wkiol1modupsbl6oaaevnznk3uq953.png "/>

Learn how to understand HGX usage.

Step three: The first step:
[[email protected] scripts]# seq  9 | sed  ' h;g '  | awk -v rs= '   ' {print $1,$2,$3,$4,$5,$6,$7,$ 8,$9} '     1        1 2        1 2 3      1 2 3 4      1 2 3 4 5    1 2 3 4 5 6    1 2 3 4 5 6 7  1 2 3 4 5 6  7 8 1 2 3 4 5 6 7 8 9 

Summary:
1.-v Define variables This variable can be used in awk
2.RS is the record separator
RS = = ""
Records is separated by runs of blank lines. Leading and trailing newlines in a file is ignored.
Each record (each row) is delimited by one or more blank lines. Line breaks in the file are ignored.

Second step: The first small step
[[email protected] scripts]# SEQ 9 | Sed ' h;g ' |  Awk-v rs= ' {for (i=1;i<=nf;i++) printf ("%d", I)} ' 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 [[email protected] scripts]#

Instead of looping the output, it's all in one line.

Summary:
    1. printf does not output line breaks by default (differs from print)

    2. You need to manually output line break "\ n" by condition (last column of each field (record))

Second small step
[[email protected] scripts]# seq 9 | sed  ' H;g '  | awk   ' Begin{ rs= ""}> {> for (i=1;i<=nf;i++) >   { if  (I==NF)       > char= "\ n" > else> char= "\ T" > printf ("%d %s", I, Char) >  }                     > } ' 1 1       2 1        2       3 1        2       3        4 1       2       3        4       5 1        2       3       4        5       6 1       2        3       4        5       6       7  1       2       3        4       5        6       7       8 1        2       3        4       5       6        7       8       9 

Shorthand for

[[email protected] scripts]# seq 9 | sed  ' H;g '  | awk -v  rs= '   ' {for (i=1;i<=nf;i++) printf ("%d %s", i,i==nf? " \ n ":" \ T ")} ' 1 1       2 1        2       3 1       2        3       4 1        2       3        4       5 1       2        3       4        5       6 1       2        3       4       5       6        7 1       2        3       4       5        6       7        8 1       2       3        4       5        6       7       8        9
Summary:

1.exp?exp1:exp2 This is a common condition operator. is also the most special of a three-mesh operator. If you don't understand the previous two sentences. Remember a word: if (condition) Then-body else else-body the same
2. Judge whether the last number of a field is I==NF.
3. If it is the last one, break the line
4. Not the output \ t

The last step is the first step

Match out

1*11*2 2*2 ... and so on.

The form

[[email protected] scripts]# seq 9 | sed  ' H;g '  | awk -v  rs= '   ' {for (i=1;i<=nf;i++) printf ("%d*%d %s", i,nr,i==nf? " \ n ":" \ T ")} ' 1*1 1*2     2*2 1*3     2*3      3*3 1*4     2*4     3*4      4*4 1*5     2*5     3*5      4*5     5*5 1*6     2*6      3*6     4*6     5*6      6*6 1*7     2*7     3*7      4*7     5*7     6*7      7*7 1*8     2*8     3*8     4*8     5*8      6*8     7*8     8*8 1*9      2*9     3*9     4*9     5*9      6*9     7*9     8*9      9*9
One last step

Calculation results

[[email protected] scripts]# seq 9 | sed  ' H;g '  | awk -v  rs= '   ' {for (i=1;i<=nf;i++) printf ("%d*%d=%d %s", i,nr,i*nr,i==nf? " \ n ":" \ T ")} ' 1*1=1 1*2=2   2*2=4 1*3=3   2*3=6   3*3=9  1*4=4   2*4=8   3*4=12  4*4=16 1*5=5   2*5= 10  3*5=15  4*5=20  5*5=25 1*6=6   2*6=12  3*6=18   4*6=24  5*6=30  6*6=36 1*7=7   2*7=14  3*7=21   4*7=28  5*7=35  6*7=42  7*7=49 1*8=8   2*8=16   3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54   7*9=63  8*9=72  9*9=81

Summary:

    1. Understand the mode space of SED, understand the line can understand it.

    2. Awk's nr= "" empty usage.

Note

Today is the 126th day of the day to accompany you and look forward to your progress .

For questions and answers, please leave a comment in the blog comments section .
Index of the topic of the previous period

http://lidao.blog.51cto.com/3388056/1914205

This article is from the "Lee blog" blog, make sure to keep this source http://lidao.blog.51cto.com/3388056/1961524

Old boy Education daily-day 126th-print multiplication tables with shell scripts

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.