awk自主練

##:writing_hand_tone1::zero:はじめに
閑職の頃に社内蔵書のオライリー本を見ながら一人遊びで覚えたawkを
久しぶりに思い出してみようと自主練習中。
何とかもっと短く書けないかと熱中してしまう悪い癖までぶり返す。
間違いとかもっと良い手とかあれば教えてください。

##:lifter_tone1::one: ずんだ問題 [ *](https://qiita.com/tbpgr/items/3a4169b370f1a317ece0)


######[a. 縦読み](https://qiita.com/tbpgr/items/90abccf8297be0e42f8c)
`gawk '{s=s substr($0,1,1)}END{print s}' `

######[b. 1桁足し算](https://qiita.com/tbpgr/items/6c75930566dda97bb5b6)
`awk 'BEGIN{for(i=1;i<10;i++)for(j=1;j<10;j++)print i"+"j"="substr(w=w"~~",1,i+j)i+j}'`

######[c. 表見出し++](https://qiita.com/tbpgr/items/427d35f981cd9675efb9)
`awk 'BEGIN{FS=OFS="|"}$1+=1'`

######[d. MARQUEE](https://qiita.com/tbpgr/items/5765ff5bcb418ae6c30e)
`gawk '$0=gensub(/<h([1-6])>(.*<\/)h[1-6]>/,"<marquee scrollamount=\"\\1\">\\2marquee>",1)'`

######[e. ハロウィン](https://qiita.com/tbpgr/items/1f2ed0e14e5d94775963)
`awk '1;$0=match($0,/r/)?"happy halloween":"sleepy"'`

######[f. 合成数列の和](https://qiita.com/advent-calendar/2018/composite-number)
`awk '{for(i=4;$0>0;i++)for(j=2;j<i;j++)k+=i%j<1&&$0--?j=i:0}$0=k;k=0'`

######[g. 菱形アルファベット](https://qiita.com/tbpgr/items/0567699ba083158804ac)
`awk '$0%2{for(r=h=$0/2-.5;h+r+1;r--)for(c=h;h+c+1;c--)printf("%c"(h+c?a:RS),h-sqrt(r^2)-sqrt(c^2)?95:h-r+97)}$0=$0%2?0:"invalid"'`

######[h. シクシク素数列](https://qiita.com/advent-calendar/2018/4949prime-series)
`awk '{for(i=9;$0;i++)if(match(i,/4|9/)){for(j=2;j<i;j++)if(i%j<1)j=i;printf(i<j?x:--$0?i",":i RS)}}'`

######[i. さくらんぼ計算](https://qiita.com/tbpgr/items/368edd0a39074ba9be04)
`awk '$0=$1","(c=10-$1)","$2-c'`

######[j. ポップル錯視](https://qiita.com/tbpgr/items/7530fda4725384c897c6)
`awk '{for(i=0;i<4;i++)t=$0t;for(i=0;i<length(t);i++)r=substr(t,i+1,1)r;for(i=1;i<12;i++)print i%3?i%6<3?t:r:n}'`

##:lifter_tone1::two: その他

######[z. ズンドコキヨシ]()
`awk -v S=${RANDOM} '!Z{D=Z;for(K=1;K<6;K++){D=D substr(S/rand(),1,K)%2;printf(D%2?"Doko\t":"Zun\t")}}!Z{Z=D-1?Y:"Ki.Yo.Shi!";printf(Z"\n")}'`

##:white_check_mark::ok:結果
###### a.
```awk:縦読み
$ echo -e "忙しくてついったいじれない\nさみしい\nつらい" | gawk '{s=s substr($0,1,1)}END{print s}'
忙さつ
```
###### b.
```awk:1桁足し算
$ awk 'BEGIN{for(i=1;i<10;i++)for(j=1;j<10;j++)print i"+"j"="substr(w=w"~~",1,i+j)i+j}'
1+1=~~2
1+2=~~~3
1+3=~~~~4
1+4=~~~~~5
1+5=~~~~~~6
1+6=~~~~~~~7
1+7=~~~~~~~~8
1+8=~~~~~~~~~9
1+9=~~~~~~~~~~10
2+1=~~~3
2+2=~~~~4
2+3=~~~~~5
2+4=~~~~~~6
2+5=~~~~~~~7
2+6=~~~~~~~~8
2+7=~~~~~~~~~9
2+8=~~~~~~~~~~10
2+9=~~~~~~~~~~~11
3+1=~~~~4
3+2=~~~~~5
3+3=~~~~~~6
3+4=~~~~~~~7
3+5=~~~~~~~~8
3+6=~~~~~~~~~9
3+7=~~~~~~~~~~10
3+8=~~~~~~~~~~~11
3+9=~~~~~~~~~~~~12
4+1=~~~~~5
4+2=~~~~~~6
4+3=~~~~~~~7
4+4=~~~~~~~~8
4+5=~~~~~~~~~9
4+6=~~~~~~~~~~10
4+7=~~~~~~~~~~~11
4+8=~~~~~~~~~~~~12
4+9=~~~~~~~~~~~~~13
5+1=~~~~~~6
5+2=~~~~~~~7
5+3=~~~~~~~~8
5+4=~~~~~~~~~9
5+5=~~~~~~~~~~10
5+6=~~~~~~~~~~~11
5+7=~~~~~~~~~~~~12
5+8=~~~~~~~~~~~~~13
5+9=~~~~~~~~~~~~~~14
6+1=~~~~~~~7
6+2=~~~~~~~~8
6+3=~~~~~~~~~9
6+4=~~~~~~~~~~10
6+5=~~~~~~~~~~~11
6+6=~~~~~~~~~~~~12
6+7=~~~~~~~~~~~~~13
6+8=~~~~~~~~~~~~~~14
6+9=~~~~~~~~~~~~~~~15
7+1=~~~~~~~~8
7+2=~~~~~~~~~9
7+3=~~~~~~~~~~10
7+4=~~~~~~~~~~~11
7+5=~~~~~~~~~~~~12
7+6=~~~~~~~~~~~~~13
7+7=~~~~~~~~~~~~~~14
7+8=~~~~~~~~~~~~~~~15
7+9=~~~~~~~~~~~~~~~~16
8+1=~~~~~~~~~9
8+2=~~~~~~~~~~10
8+3=~~~~~~~~~~~11
8+4=~~~~~~~~~~~~12
8+5=~~~~~~~~~~~~~13
8+6=~~~~~~~~~~~~~~14
8+7=~~~~~~~~~~~~~~~15
8+8=~~~~~~~~~~~~~~~~16
8+9=~~~~~~~~~~~~~~~~~17
9+1=~~~~~~~~~~10
9+2=~~~~~~~~~~~11
9+3=~~~~~~~~~~~~12
9+4=~~~~~~~~~~~~~13
9+5=~~~~~~~~~~~~~~14
9+6=~~~~~~~~~~~~~~~15
9+7=~~~~~~~~~~~~~~~~16
9+8=~~~~~~~~~~~~~~~~~17
9+9=~~~~~~~~~~~~~~~~~~18
```
###### c.
```awk:表見出し++
$ echo -e "0|col1|col2|col3\n1|col1|col2|col3\n2|col1|col2|col3\n3|col1|col2|col3\n4|col1|col2|col3" | awk 'BEGIN{FS=OFS="|"}$1+=1'
1|col1|col2|col3
2|col1|col2|col3
3|col1|col2|col3
4|col1|col2|col3
5|col1|col2|col3
```
###### d.
```awk:MARQUEE
$ echo -e "<h1>test</h1>\n<h2>test</h2>\n<h3>test</h3>\n<div>div test1</div>\n<div>div test2</div>\n<div>div test3</div>" | gawk '$0=gensub(/<h([1-6])>(.*<\/)h[1-6]>/,"<marquee scrollamount=\"\\1\">\\2marquee>",1)'
<marquee scrollamount="1">test</marquee>
<marquee scrollamount="2">test</marquee>
<marquee scrollamount="3">test</marquee>
<div>div test1</div>
<div>div test2</div>
<div>div test3</div>
```
###### e.
```awk:ハロウィン
$ echo $RANDOM | awk '{split("trick treat sushi poyayou tamakatsu",a)}$0=a[1+$0%5]' | awk '1;$0=match($0,/r/)?"happy halloween":"sleepy"'
trick
happy halloween
```
###### f.
```awk:合成数列の和
$ seq 10 | awk '{for(i=4;$0>0;i++)for(j=2;j<i;j++)k+=i%j<1&&$0--?j=i:0}$0=k;k=0'
4
10
18
27
37
49
63
78
94
112
```
###### g.
```awk:菱形アルファベット
$ seq 10 | awk '$0%2{for(r=h=$0/2-.5;h+r+1;r--)for(c=h;h+c+1;c--)printf("%c"(h+c?a:RS),h-sqrt(r^2)-sqrt(c^2)?95:h-r+97)}$0=$0%2?0:"invalid"'
a
invalid
_a_
b_b
_c_
invalid
__a__
_b_b_
c___c
_d_d_
__e__
invalid
___a___
__b_b__
_c___c_
d_____d
_e___e_
__f_f__
___g___
invalid
____a____
___b_b___
__c___c__
_d_____d_
e_______e
_f_____f_
__g___g__
___h_h___
____i____
invalid
```
###### h.
```シクシク素数列
$ seq 10 | awk '{for(i=9;$0;i++)if(match(i,/4|9/)){for(j=2;j<i;j++)if(i%j<1)j=i;printf(i<j?x:--$0?i",":i RS)}}'
19
19,29
19,29,41
19,29,41,43
19,29,41,43,47
19,29,41,43,47,59
19,29,41,43,47,59,79
19,29,41,43,47,59,79,89
19,29,41,43,47,59,79,89,97
19,29,41,43,47,59,79,89,97,109
```
```more4949(almost_sick)
$ seq 4 9 | awk '{for(c=4+9;$(sick*sick);c++)if(match(c,/4|9/)){for(q=sqrt(49)-sqrt(4)-sqrt(9);c>q;q++)if(c%q<49/49)q=c;printf(c<q?sick:--$(sick*sick)?c",":q RS)}}'
19,29,41,43
19,29,41,43,47
19,29,41,43,47,59
19,29,41,43,47,59,79
19,29,41,43,47,59,79,89
19,29,41,43,47,59,79,89,97
```
###### i.
```さくらんぼ計算
$ echo -e "1 9\n3 9\n8 5" | awk '$0=$1","(c=10-$1)","$2-c'
1,9,0
3,7,2
8,2,3
```
###### j.
```ポップル錯視
$ echo "杏マナー" | awk '{for(i=0;i<4;i++)t=$0t;for(i=0;i<length(t);i++)r=substr(t,i+1,1)r;for(i=1;i<12;i++)print i%3?i%6<3?t:r:n}'
杏マナー杏マナー杏マナー杏マナー
杏マナー杏マナー杏マナー杏マナー

ーナマ杏ーナマ杏ーナマ杏ーナマ杏
ーナマ杏ーナマ杏ーナマ杏ーナマ杏

杏マナー杏マナー杏マナー杏マナー
杏マナー杏マナー杏マナー杏マナー

ーナマ杏ーナマ杏ーナマ杏ーナマ杏
ーナマ杏ーナマ杏ーナマ杏ーナマ杏
```

###### z.
```ズンドコキヨシ
seq 100 | awk -v S=${RANDOM} '!Z{D=Z;for(K=1;K<6;K++){D=D substr(S/rand(),1,K)%2;printf(D%2?"Doko\t":"Zun\t")}}!Z{Z=D-1?Y:"Ki.Yo.Shi!";printf(Z"\n")}'
Doko    Doko    Doko    Doko    Doko
Zun     Doko    Zun     Doko    Doko
Doko    Doko    Zun     Zun     Zun
Doko    Zun     Doko    Zun     Zun
Zun     Zun     Zun     Doko    Doko
Doko    Zun     Doko    Doko    Doko
Zun     Zun     Zun     Doko    Zun
Doko    Doko    Zun     Doko    Doko
Doko    Doko    Doko    Zun     Doko
Zun     Doko    Zun     Zun     Zun
Zun     Doko    Doko    Zun     Doko
Zun     Doko    Zun     Doko    Zun
Doko    Doko    Zun     Zun     Zun
Zun     Zun     Zun     Doko    Doko
Zun     Doko    Doko    Doko    Doko
Doko    Zun     Zun     Doko    Doko
Zun     Zun     Zun     Zun     Doko    Ki.Yo.Shi!
```