Indexへ
(15626)//【15627】→(15628)
------------------------
【タイトル】@gawk と @ruby による外部変形について
【記事番号】 15627 (*)
【 日時 】09/08/16 14:03
【 発言者 】Kazuo Miyake qqza3hq89@aioros.ocn.ne.jp
【 リンク 】http://www17.ocn.ne.jp/~lite/



 awk や ruby の 外部変形で jwc_temp.txt
を 読み込みながら jwc_temp.txt に 書き込
む やり方を 報告します。

 パターンは
 type file | @gawk 〜 > file
 type file | @ruby 〜 > file
です。

 mawk も 使えます。例えば
 type file | @c:\jww\mawk32 〜 > file
のようにします。


 以下に利用例を紹介します。

○ hq と hd を入れ換える処理です。

:hq と hd を入れ換える
@echo off
REM #jw
REM #e

: [ awk ] '〜' 処理系
type jwc_temp.txt | @gawk '{ sub(/^^^^hq/,"hd"); print }' > jwc_temp.txt

: [ awk ] "〜" 処理系
type jwc_temp.txt | @gawk "{ sub(/^hq/,\"hd\"); print }" > jwc_temp.txt

: [ ruby ]
type jwc_temp.txt | @ruby -pe "$_.sub!(/^hq/,'hd')" > jwc_temp.txt


○ 線を引くプログラムです。

:線を引く
@echo off
REM #jw
REM #1-%d 始点を指示してください。 (L)free (R)Read
REM #2%d 終点を指示してください。 (L)free (R)Read
REM #e

: [ awk ] '〜' 処理系
type jwc_temp.txt | @gawk ^
'^
/^^^^hp[12]/{ print $2,$3 }^
}^
' > jwc_temp.txt

: [ awk ] "〜" 処理系
type jwc_temp.txt | @gawk ^
"^
/^^hp[12]/{ print $2,$3 }^
}^
" > jwc_temp.txt

: [ ruby ]
type jwc_temp.txt | @ruby -nx %~f0 > jwc_temp.txt
goto:eof
#!ruby -Ks
puts $_.split[1,2].join(' ') if $_ =~ /^hp[12]/
__END__


 @gawk '〜' 処理系は シェルの影響を強く
受けるようです。

 そのほかにも いろいろなやり方があると
思います。


 皆様方のご健康とご活躍をお祈り申し上
げます。


       記録 2009.08.16 三宅


Indexへ
(15627)←【15628】→(15638)
------------------------
【タイトル】Re(1):@gawk と @ruby による外部変形について
【記事番号】 15628 (15627)
【 日時 】09/08/17 19:21
【 発言者 】why

どんな場合に使うのか教えてください。


Indexへ
(15628)←【15638】→(15636)
------------------------
【タイトル】Re(2):@gawk と @ruby による外部変形について
【記事番号】 15638 (15628)
【 日時 】09/08/21 19:48
【 発言者 】Kazuo Miyake qqza3hq89@aioros.ocn.ne.jp
【 リンク 】http://www17.ocn.ne.jp/~lite/



▼whyさん:
>どんな場合に使うのか教えてください。

このやり方の外部変形を、実際に、仕事で使っているわけではありません。
実用的な、使い方を紹介することはできないのですが、このやり方の考え方
は、つぎのとおりです。

通常 awk や ruby の 外部変形は

copy jwc_temp.txt jwc_temp.bak > nul
外部変形 jwc_temp.bak > jwc_temp.txt

あるいは

外部変形 jwc_temp.txt > jwc_temp.bak
del jwc_temp.txt
ren jwc_temp.bak jwc_temp.txt

のように jwc_temp.bak のような 一時ファイル
を使いますが、 @gawk や @ruby を使えば
つぎのようなパターンの外部変形が作れます。

type jwc_temp.txt | @外部変形 > jwc_temp.txt


また
gawk は つぎのようなパターンも考えられます。

外部変形 jwc_temp.txt | @more > jwc_temp.txt

○ hq と hd を入れ換える処理です。

:hq と hd を入れ換える
@echo off
REM #jw
REM #e
gawk '{ sub(/^^hq/,"hd"); print }' jwc_temp.txt | @more > jwc_temp.txt


このやり方は powershell は つぎのようなパターンで

(type jwc_temp.txt) | 外部変形 > jwc_temp.txt

処理できるようなので、awk や ruby ならどうなるか
考えた結果の報告です。

質問ありがとうございました。
答にはなっていないかもしれませんが、おゆるしください。


Indexへ
(15638)←【15636】→(15639)
------------------------
【タイトル】Re(1):@gawk と @ruby による外部変形について
【記事番号】 15636 (15627)
【 日時 】09/08/20 21:37
【 発言者 】いっち

Kazuo Miyakeさんのホームページは、
JScriptで外部変形をつくる時に、勉強にさせて頂きました。
--------------------------------------------------------------
本来は、awk、rubyの他、VBScript、JScriptなどで、
作成した方が良いと思われる外部変形をあえて、
エクセルでつくってみました。

掲示板にコードを貼ると、半角スペースが全角スペースに変わって
しまっているかもかもしれません。

---- Ex線をひく.bat ---- ↓この下から

:Ex線を引く
@echo off
REM #jw
REM #cd
REM #1- 始点を指示してください。 (L)free (R)Read
REM #2 終点を指示してください。 (L)free (R)Read
REM #e
WScript Ex線を引く.vbs

------------------------ ↑この上まで
---- Ex線を引く.vbs ---- ↓この下から

Option Explicit
Private myBookName, myBook
myBookName = "Ex線を引く.xls"
With WScript
    myBook = Replace(.ScriptFullName, .ScriptName, myBookName)
End With
With CreateObject("Excel.Application")
    
    '正常な動作確認がとれるまでは、
    '.Visible = True を有効にしておいてください。
    '正常な動作確認がとれたら、
    '.Visible = True は不要です。(コメント行にしてください。)
    
    .Visible = True
    
    .Workbooks.Open myBook, 0, True
End With

------------------------ ↑この上まで
---- Ex線を引く.xls ---- ↓この下から

'すべて、ThisWorkBookモジュールに、下記コードを貼り付けます。

Option Explicit

Private Function StrRepRegExp _
  (StrText As String, StrPattern As String, StrRep As String) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    .Pattern = StrPattern
    StrRepRegExp = .Replace(StrText, StrRep)
  End With
End Function

Private Sub Workbook_Open()
  If Me.ReadOnly Then
    Start
    Application.Quit
  End If
End Sub

Sub Start()
  Dim myFile As String
  Dim myFNo As Integer
  Dim ss As String
  Dim f As Boolean
  
  On Error GoTo errhdl
  myFile = ThisWorkbook.Path & Application.PathSeparator & "JWC_TEMP.TXT"
  myFNo = FreeFile
  Open myFile For Input As #myFNo
  f = True
  ss = StrConv(InputB(LOF(myFNo), #myFNo), vbUnicode)
  Close #myFNo: f = False
  ss = StrRepRegExp(ss, "^hq", "hd")
  ss = StrRepRegExp(ss, "^(hp1-\s+|hp2\s+)|(\r\n)+$", "")
  myFNo = FreeFile
  Open myFile For Output As #myFNo
  f = True
  Print #myFNo, ss
  Close #myFNo: f = False
  Exit Sub
errhdl:
  Err.Clear
  If f Then Close #myFNo: f = False
  With Application
    If Not .Visible Then
      .Visible = True
    End If
  End With
End Sub

------------------------ ↑この上まで


Indexへ
(15636)←【15639】//(15629)
------------------------
【タイトル】Re(2):@gawk と @ruby による外部変形について
【記事番号】 15639 (15636)
【 日時 】09/08/21 20:03
【 発言者 】Kazuo Miyake qqza3hq89@aioros.ocn.ne.jp
【 リンク 】http://www17.ocn.ne.jp/~lite/



▼いっちさん:
すごいですね。きれいです。
すばらしい副産物が、収穫できるといいですね。

線が引けると、理論的には図面が書けるわけですから。
共感します。