第18回 シェル芸勉強会

夏の終わりにふらっと参加してきました。

上田ブログ
https://blog.ueda.asia/?p=6836

今回は cat，comm，grep，uniq を利用するものが多かった気がします。個々のコマンドの解説はくんすとさんの記事がまとまっています。

[「第18回ニンニク入れますかシェル芸勉強会」についったーで参加しました](http://kunst1080.hatenablog.com/entry/2015/08/30/164448)

勉強会で配布されたファイルは Q7 以外は文字コードを Shift_JIS に変換して利用しました。ファイル名も自分のこのみで変更してあります。

```bat:A1.bat
@echo off

set SELF=%~n0

setlocal
  :: ref. http://scripting.cocolog-nifty.com/blog/2009/09/uniq-77dd.html
  copy NUL %SELF%.1.tmp > NUL
  for /f "tokens=*" %%i in ('findstr /r "オ.*ン" Q1.txt') do (
    findstr /x /c:"%%i" %SELF%.1.tmp > NUL || (echo %%i) >> %SELF%.1.tmp
  )
  
  :: ref. http://scripting.cocolog-nifty.com/blog/2011/07/dupuniq--d-39a1.html
  copy NUL %SELF%.2.tmp > NUL
  for /f "tokens=1" %%i in (%SELF%.1.tmp) do (
    for /f %%j in ('findstr /b /c:"%%i" ^< %SELF%.1.tmp ^| find /c /v ""') do (
      if %%j gtr 1 (
        findstr /b /c:"%%i" < %SELF%.2.tmp > NUL || (echo %%i) >> %SELF%.2.tmp
      )
    )
  )
  
  type %SELF%.2.tmp
  
  del /q %SELF%.*.tmp
endlocal
```

```bat:A2.bat
@echo off

setlocal
  :: ref. http://scripting.cocolog-nifty.com/blog/2009/09/comm1-9073.html
  echo.
  echo Q2.1.txt のみ存在する行
  findstr /x /l /v /g:Q2.2.txt Q2.1.txt
  
  echo.
  echo Q2.2.txt のみ存在する行
  findstr /x /l /v /g:Q2.1.txt Q2.2.txt
  
  :: ref. http://scripting.cocolog-nifty.com/blog/2009/09/comm3-4b26.html
  echo.
  echo 両方に存在する行
  findstr /x /l /g:Q2.1.txt Q2.2.txt
endlocal
```

```bat:A3.bat
@echo off

setlocal enabledelayedexpansion
  for %%i in (Q3.*.txt) do (
    set n=0
    for /f "tokens=*" %%j in (%%i) do (
      for %%k in (%%j) do (
        set /a n+=%%k
      )
    )
    echo %%i: !n!
  )
endlocal
```

```bat:A4.bat
@echo off

setlocal enabledelayedexpansion
  set h=
  for /f "tokens=*" %%i in (Q4.txt) do (
    set h=%%i
    goto :BREAK
  )
  :BREAK
  
  set h=%h:~1%
  set i=0
  set t=
  :BOL
    call set c=%%h:~%i%,1%%
    if "%c%" == "" goto EOL
    set t=%t% %c%
    set /a i+=1
    goto BOL
  :EOL
  
  for /f "tokens=*" %%i in ('findstr /l "x" Q4.txt') do (
    set s=%%i
    
    set i=1
    for %%j in (%t%) do (
      call set c=%%s:~!i!,1%%
      if "!c!" == "x" (
        echo !s:~0,1!-%%j
      )
      set /a i+=1
    )
  )
endlocal
```

```bat:A5.bat
@echo off

setlocal
  more /s Q5.txt
endlocal

```

```bat:A6.bat
@echo off

set SELF=%~n0

setlocal enabledelayedexpansion
  set x=8
  set /a y=%x%/2
  
  (
    echo P2 %x% %x% 1
    for /l %%i in (1,1,%y%) do (
      set t=
      for /l %%j in (1,1,%x%) do (
        set /a m=%%j%%2
        if !m! equ 0 (set t=!t!0 ) else (set t=!t!1 )
      )
      echo !t:~0,-1!
      echo !t:~2,-1! 1
    )
  ) > %SELF%.pgm
  
  type %SELF%.pgm
  
  del /q %SELF%.pgm
endlocal

```

```bat:A7.bat
@echo off

set SELF=%~n0

setlocal enabledelayedexpansion
  chcp 65001 > NUL
  
  copy NUL %SELF%.tmp > NUL
  for /f "tokens=*" %%i in (Q7.txt) do (
    call :getc "%%i" >> %SELF%.tmp
  )
  
  for /f "tokens=*" %%i in (%SELF%.tmp) do (
    for /f "tokens=3" %%j in ('find /c "%%i" Q7.txt') do (
      if %%j gtr 1 (
        set c=%%i
      )
    )
  )
  
  find /n "%c%" Q7.txt
  
  echo.
  pause
  
  del /q %SELF%.tmp
  
  chcp 932 > NUL
endlocal
goto :EOF

:getc
setlocal
  set s=%~1
  
  set i=0
  :BOL
    call set c=%%s:~%i%,1%%
    if "%c%" == "" goto EOL
    echo %c%
    set /a i+=1
    goto BOL
  :EOL
endlocal
goto :EOF
```

```bat:A8.bat
@echo off

set SELF=%~n0

setlocal enabledelayedexpansion
  copy NUL %SELF%.1.tmp > NUL
  for /f "tokens=*" %%i in (Q8.txt) do (
    set s=%%i
    goto BREAK
  )
  :BREAK
  
  call :length "%s%"
  set n=%ERRORLEVEL%
  
  :: KLUDGE
  :: for /l %%i in (1,1,%n%) do (
  for /l %%i in (1,1,3) do (
    for /l %%j in (1,1,%n%) do (
      set t=!s:~%%j,%%i!
      call :length "!t!"
      if !ERRORLEVEL! equ %%i (
        >> %SELF%.1.tmp echo !t!
      )
    )
  )
  
  copy NUL %SELF%.2.tmp > NUL
  for /f "tokens=*" %%i in (%SELF%.1.tmp) do (
    for /f %%j in ('findstr /x /c:"%%i" ^< %SELF%.1.tmp ^| find /c /v ""') do (
      if %%j gtr 1 (
        findstr /x /c:"%%i" < %SELF%.2.tmp > NUL || (echo %%i) >> %SELF%.2.tmp
      )
    )
  )
  
  type %SELF%.2.tmp
  
  del /q %SELF%.*.tmp
endlocal
goto :EOF

:length
setlocal
  set s=%~1
  
  if "%s%" == "" (
    exit /b 0
  )
  
  set i=0
  :BOL
    call set c=%%s:~%i%,1%%
    if "%c%" == "" goto EOL
    set /a i+=1
    goto BOL
  :EOL
  
  exit /b %i%
endlocal
goto :EOF
```

当日は「オカシ！オカシナイノー？オカシカッテクルカラオカネチョーダイ！」といって、会長さんみずからお菓子を買いこんで朝からむしゃむしゃ食べておられました（すごくパワフルでした）