.txtファイルを解析して.csvを生成します。

.txtファイルを解析して.csvを生成します。

次の内容を含むテキストファイルがあります。

Torrent file  : Linux.Format.-.October.2016.-.True.Pdf.-.Set.1001.[ECLiPSE].torrent
Metadata info : 9968 bytes, 412 pieces, 65536 bytes per piece
Torrent name  : Linux Format - October 2016 - True Pdf - Set 1001 [ECLiPSE]
Content info  : 3 files, 26965176 bytes
Announce URL  : http://explodie.org:6969/announce

F#  Bytes       File name
--- ----------- ---------------------------------------------------------------
  1    26944026 linfor1016.pdf
  2       19963 ECLiPSE.txt
  3        1187 Read Me.txt

Torrent file  : linuxmint-13-cinnamon-dvd-64bit.iso.torrent
Metadata info : 32303 bytes, 1602 pieces, 524288 bytes per piece
Torrent name  : linuxmint-13-cinnamon-dvd-64bit.iso
Content info  : single file, 839909376 bytes
Announce URL  : http://torrents.linuxmint.com/announce.php
Torrent file  : linuxmint-13-kde-dvd-64bit.iso.torrent
Metadata info : 35938 bytes, 1784 pieces, 524288 bytes per piece
Torrent name  : linuxmint-13-kde-dvd-64bit.iso
Content info  : single file, 935329792 bytes
Announce URL  : http://torrents.linuxmint.com/announce.php

ファイルは次のように生成されます。

for i in *.torrent;do torrentcheck -t $i >> info.txt;done

これでcsvファイルを取得できるように、このtxtファイルを変換したいと思います。2列今すぐ急流フ​​ァイル&コンテンツ情報(ヘッダーとして)上記のbashコマンドで解析された各急流ファイルについて、たとえば、次のようにします。

Torrent file,Content info 
Linux.Format.-.October.2016.-.True.Pdf.-.Set.1001.[ECLiPSE].torrent,3 files, 26965176 bytes
linuxmint-13-cinnamon-dvd-64bit.iso.torrent,single file, 839909376 bytes
linuxmint-13-kde-dvd-64bit.iso.torrent,single file, 935329792 bytes

その後、これらの列はスプレッドシートアプリケーションでさらに処理され、サイズやファイル数に基づいて急流を並べ替えることができます。

次のファイル文字列を検索できます。

grep 'Torrent file' info.txt or grep 'Content' info.txt

しかし、戻りテキスト文字列を使用して私が得たものなどの必要な情報を抽出するにはどうすればよいですか?Torrent file : linuxmint-13-cinnamon-dvd-64bit.iso.torrentスプレッドシートMID、LENコマンドを使用して、文字列を次のように減らすことができます。linuxmint-13-cinnamon-dvd-64bit.iso.torrent

ベストアンサー1

単純なawkスクリプトは、次のようにデータを解析できます。

awk -F': ' 'BEGIN { print "Torrent file,Content info,Size" }
$0~/^Torrent file/ { save = $2 }
$0~/^Content info/ { printf "%s,%s\n",save,$2 }'  <info.txt

「:」で行を分割し、行の2番目のフィールドを保存し、後で別の行が見つかった場合は印刷します。

おすすめ記事