Sed prints the 9-9 multiplication table, and sed prints the multiplication.

Source: Internet
Author: User

Sed prints the 9-9 multiplication table, and sed prints the multiplication.

<Span style = "font-size: 18px;"> 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")} '</span>

Prerequisites:

Seq: generate a sequence

Sed: mode space and storage space

H: append the content in pattern space to hold space \ n.

G: overwrite the data in the bucket.


The mode space used for output. The storage space is only a temporary storage space.

1. seq generates 1 and then adds it to the sed mode space. H appends the value of the mode space to the bucket. In this case, the mode space is 1, the storage space is \ n1, and then copies it to the mode space, at this time, the mode space is \ n1, and the final output is passed to awk.

Awk-v RS =''

'

{

For (I = 1; I <NF; I ++ ){

Printf ("% dx % d = % d % s", I, NR, I * NR, I = NR? '\ N':' \ t ');

}

}

'

Note: RS = ''indicates that empty rows are used as separators.

2. seq generates 2 and then adds it to the mode space. In this case, the mode space is 2, the storage space is \ n1, and H is 2 to the storage space. In this case, the storage space is \ n1 \ n2;

Then g copies \ n1 \ n2 to the mode space. At this time, the mode space is \ n1 \ n2 and is output to awk. At this time, awk processes \ n1 \ n2.

3. Repeat the above process to learn how to print the 9-9 multiplication table

Although 9-9 multiplication tables are implemented in one sentence, it is still a bit difficult compared to advanced languages!

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.