たいちょーの雑記

ぼくが3日に一度くらい雑記をかくところ

シェル芸160ノック25

9月末に発売されたシェル芸本の続きです。組み合わせの列挙系は楽しいですね。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題132

$ cat ./qdata/132/httpd-access.log | awk '$(NF-1)==500' | while read L; do mail -s "500があったよ" こ こにメールアドレス <<< $L; done

問題133

$ echo "@reboot sleep 180s && shutdown -h now" | sudo crontab

問題134

$ grep -vnPe "^( {4})*[^ ]|^$" qdata/134/hoge.py
5:     print(i+1)

$ autopep8 qdata/134/hoge.py --select E11 --diff
--- original/qdata/134/hoge.py
+++ fixed/qdata/134/hoge.py
@@ -2,5 +2,5 @@

 for i in range(1,10):
     print(i)
-     print(i+1)
+    print(i+1)
     print(i+2)

autopep8解はもうちょっと成形してやる方が題意通りだけど、diffの方がいいかなって

問題135

cat qdata/135/sample.lisp | grep defun | ocs "F0.Count(c=>c=='(')!=F0.Count(c=>c==')'){println(F[2])}"
fib
sum1

問題136

$ cat qdata/136/somecode.c | awk '/^#/{h=h$0"\n"}/void a/,/^\}/{a=a$0"\n"}/int b/,/^\}/{b=b$0"\n"}/main/,/^}/{m=m$0"\n"}END{print h;print a;print b;print m}'
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void a()
{
        int i = 0, j = b();
        for (; i < j; i++){
                puts("a");
        }
}

int b()
{
        return rand()%10;
}

int main(int argc, char const *argv[])
{
        srand(time(NULL));
        a();
        return 0;
}

問題137

$ clang-format ./qdata/137/fib.c
#include <stdio.h>

int fib(int n) {
  if (n <= 1) {
    return n;
  }
  return fib(n - 1) + fib(n - 2);
}

int main(void) {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d\n", fib(i));
  }
  return 0;
}

問題138

$ cat os.csv|tr , \\n| join -j9 - <(cat service.csv|tr , \\n) | join -j9 - <(cat browser.csv|tr , \\n)| awk '!($1!="Windows"&&$3=="IE")&&!($1!="macOS"&&$3=="Safari")'
 Windows ServiceA IE
 Windows ServiceA Chrome
 Windows ServiceA FireFox
 Windows ServiceB IE
 Windows ServiceB Chrome
 Windows ServiceB FireFox
 Windows ServiceC IE
 Windows ServiceC Chrome
 Windows ServiceC FireFox
 macOS ServiceA Chrome
 macOS ServiceA FireFox
 macOS ServiceA Safari
 macOS ServiceB Chrome
 macOS ServiceB FireFox
 macOS ServiceB Safari
 macOS ServiceC Chrome
 macOS ServiceC FireFox
 macOS ServiceC Safari
 Linux ServiceA Chrome
 Linux ServiceA FireFox
 Linux ServiceB Chrome
 Linux ServiceB FireFox
 Linux ServiceC Chrome
 Linux ServiceC FireFox

問題139

$ echo https://{,{,{,cc.}bb.}aa.}example.com/A{/B{/C,},} | fmt -1
https://example.com/A/B/C
https://example.com/A/B
https://example.com/A
https://aa.example.com/A/B/C
https://aa.example.com/A/B
https://aa.example.com/A
https://bb.aa.example.com/A/B/C
https://bb.aa.example.com/A/B
https://bb.aa.example.com/A
https://cc.bb.aa.example.com/A/B/C
https://cc.bb.aa.example.com/A/B
https://cc.bb.aa.example.com/A

一般解じゃないのでダメかな?

問題140

$ mkdir tmp
$ cp ./dir.tar.gz ./tmp/
$ cd ./tmp
$ tar xvfz ./dir.tar.gz
$ rm ./dir.tar.gz
$ shuf -i 0-255 -rn 400 | paste - - - - -d. | paste - <(dateutils.dseq --format "- - [%Y/%m/%d %H:%M:%S +0900] \"GET" 2021-01-01T00:00:00 1s 2021-12-31T23:59:59 | shuf -n 100 | sort) | paste -d ' ' - <(fd . ./ --exec echo /{}|shuf -rn100) | sed 's@$@ HTTP/1.1" 200@' | paste -d ' ' - <(shuf -i 100-1000 -rn100)
225.20.241.13   - - [2021/01/09 05:13:30 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 272
208.207.0.117   - - [2021/01/13 12:36:05 +0900] "GET /product.php HTTP/1.1" 200 593
14.11.149.123   - - [2021/01/19 04:56:24 +0900] "GET /cart.php HTTP/1.1" 200 153
242.122.183.51  - - [2021/01/21 16:38:16 +0900] "GET /search.php HTTP/1.1" 200 334
220.85.189.142  - - [2021/01/24 16:01:08 +0900] "GET /js/main.js HTTP/1.1" 200 494
122.200.73.176  - - [2021/01/25 18:47:54 +0900] "GET /index.php HTTP/1.1" 200 266
22.80.42.4      - - [2021/02/02 04:45:09 +0900] "GET /img HTTP/1.1" 200 757
129.176.186.244 - - [2021/02/04 06:21:02 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 575
203.238.5.90    - - [2021/02/07 22:17:50 +0900] "GET /js/main.js HTTP/1.1" 200 484
233.1.189.253   - - [2021/02/08 19:48:11 +0900] "GET /img HTTP/1.1" 200 273
63.156.210.229  - - [2021/02/18 15:54:29 +0900] "GET /js/bootstrap.js HTTP/1.1" 200 530
149.201.91.75   - - [2021/03/06 01:26:53 +0900] "GET /js/main.js HTTP/1.1" 200 716
...

dseqが激重なので範囲減らすかファイルに書き出すといいと思いますね