9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第18回 シェル芸勉強会

Last updated at Posted at 2015-08-30

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

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

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

「第18回ニンニク入れますかシェル芸勉強会」についったーで参加しました

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

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
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
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
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
A5.bat
@echo off

setlocal
  more /s Q5.txt
endlocal

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

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
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

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

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?