たいちょーの雑記

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

シェル芸160ノック11

9月末に発売されたシェル芸本の続きです。

gihyo.jp

前回はこちら

xztaityozx.hatenablog.com

問題52

awk '{print FILENAME,$0}' qdata/52/* | awk '$2=="*"{for(i=3;i<=NF;i++)a[i]=$i}$2!="*"{for(k in a) print $1,$2,a[k],$k}' | sel -d_ 2
U X A 4
U X B 2
U Y A 3
U Y B 1
V X A 7
V X B 6
V X C -1
V Y A 9
V Y B 8
V Y C -2

問題53

$ (cat ./qdata/53/devicelist.txt;awk '{print "@",$1}' ./qdata/53/measurement.txt) | sort -k2 | uniq -f1 | join - <((sel 2 1 -f ./qdata/53/measurement.txt;awk '{print "@",$2}' ./qdata/53/devicelist.txt) | sort -k2 | uniq -f1) -1 2 -2 2 | sel 2 1 3 | sort -k1n
@ xxxx.0d81.33a8 1607
@ xxxx.17d0.2c07 3431
01 xxxx.0c4d.1c45 1914
02 xxxx.0d46.f3c2 @
03 xxxx.0d17.73a6 2275
04 xxxx.0d81.33b8 @
05 xxxx.0d17.9658 @
06 xxxx.0c4d.095c 3235
07 xxxx.0a69.b711 3119
08 xxxx.0d81.1da2 @
09 xxxx.0fff.d828 3618
10 xxxx.0d17.7478 3443

問題54

$ jq -rc '.Fruits[] | .Name+".json",.' ./qdata/54/fruits.json | paste - - -d ' ' | while read F L; do echo $L|jq > $F; done

問題55

$ (echo \[;sed 's/$/,/' qdata/55/watch_log.json;echo {}\]) | gron | grep -Pe "timestamp|output" | sel 3: | sd '\\' '\\\' | while read L; do read T; [[ "$tt" == "" ]] &&{tt=$T;ll=$L;} || { echo diff: $tt $T; echo -e $ll | diff - <(echo -e $L);tt=$T;ll=$L; }; done
diff: "2020-01-18 18:07:34"; "2020-01-18 18:06:52";
1,3c1,3
< total 9888204
< drwxr-xr-x  19 uesugi staff        608  1 18 18:07 .
< drwxr-xr-x+ 86 uesugi staff       2752  1 18 18:07 ..
---
> "total 9888204
> drwxr-xr-x  19 uesugi staff        608  1 12 23:22 .
> drwxr-xr-x+ 86 uesugi staff       2752  1 18 18:06 ..
21c21
< ;
---
> ";
diff: "2020-01-18 18:06:52"; "2020-01-18 18:07:06";
3c3
< drwxr-xr-x+ 86 uesugi staff       2752  1 18 18:06 ..
---
> drwxr-xr-x+ 86 uesugi staff       2752  1 18 18:07 ..
diff: "2020-01-18 18:07:06"; "2020-01-18 18:07:20";
2c2
< drwxr-xr-x  19 uesugi staff        608  1 12 23:22 .
---
> drwxr-xr-x  20 uesugi staff        640  1 18 18:07 .
19a20
> -rw-r--r--   1 uesugi staff          0  1 18 18:07 aaa
diff: "2020-01-18 18:07:20"; "2020-01-18 18:07:26";
2c2
< drwxr-xr-x  20 uesugi staff        640  1 18 18:07 .
---
> drwxr-xr-x  21 uesugi staff        672  1 18 18:07 .
20a21
> -rw-r--r--   1 uesugi staff          0  1 18 18:07 bbb
diff: "2020-01-18 18:07:26"; "2020-01-18 18:07:32";
2c2
< drwxr-xr-x  21 uesugi staff        672  1 18 18:07 .
---
> drwxr-xr-x  20 uesugi staff        640  1 18 18:07 .
20d19
< -rw-r--r--   1 uesugi staff          0  1 18 18:07 aaa
diff: "2020-01-18 18:07:32"; "2020-01-18 18:07:34";
2c2
< drwxr-xr-x  20 uesugi staff        640  1 18 18:07 .
---
> drwxr-xr-x  19 uesugi staff        608  1 18 18:07 .
20d19
< -rw-r--r--   1 uesugi staff          0  1 18 18:07 bbb

うーむこれは確かに別ファイルにそれぞれ書き出してdiffした方がよかったかもしれない

問題56

$ sed -e "$(cat qdata/56/service_stop_weekday.txt | awk -F: '{printf "s/%s/%s/;", $1,$2}' )" ./qdata/56/service_depend_list.txt | sel --remove-empty -gd :\|\\s\|, 0 | awk '{for(i=2;i<=NF;i++)a[$i]=$1","a[$i]}END{for(k in a)print k,a[k]}' | while read D L; do echo $D "$(sd , \\n <<< $L|sort -u|sed '/^$/d'|paste -sd ,)"; done | sed 'y/月火水木金土日/1234567/' | sort -k1 | sed 'y/1234567/月火水木金土日/;s/^./&:/' | sd , ', '
月: アプリA, アプリC, アプリD
火: アプリB, アプリC, アプリE
水: アプリB, アプリC, アプリE
木: アプリA
金: アプリC, アプリD
土: アプリA, アプリB, アプリC, アプリD, アプリE
日: アプリB, アプリE

長くなってまいりました