1

I have a text file with following contents

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

file is generated via

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

Now I want to convert this txt file such that I get a csv file with two columns, namely Torrent file & Content info (as headers) for each torrent file parsed via above bash command like:

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

the columns can then be further processed in any Spreadsheet application to sort torrents by their size or by the number of files in them...

I can search for file strings like

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

but how do I play with the return text strings to extract required info like if I get Torrent file : linuxmint-13-cinnamon-dvd-64bit.iso.torrent I can use Spreadsheet MID,LEN commands to reduce string to just linuxmint-13-cinnamon-dvd-64bit.iso.torrent

2

A simple awk script can parse the data, eg:

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

Split the lines on ": ", save the 2nd field of one line, print it later when the other line is found.

  • You could roll this into a 'oneliner' ala echo "Torrent file,Content info" > info.csv && cat info.txt | awk -F': ' '$1=="Torrent file " { save = $2 } $1=="Content info " { printf "%s,%s\n",save,$2 }' >> info.csv – FCTW Nov 9 '16 at 17:22
  • thnx for replying, however your oneliner solution is outputting only the first line that is Torrent file,Content info nothing else ..I am running your command after cd'ing to directory containing info.txt – nightcrawler Nov 9 '16 at 17:34
  • @nightcrawler I edited my answer to be less fussy with the number of spaces before the ":", and begin with the column header. Perhaps that helps. With this pattern the T of Torrent must be the first character on the line, with no intervening spaces, for example. – meuh Nov 9 '16 at 17:42
  • I don't know is it my bash alone ..however your original answer (separated lines) did work ..and tinkering finally I got this to work echo "Torrent file,Content info" > info.csv && cat info.txt | awk -F': ' ' $1=="Torrent file " { save = $2 } $1=="Content info " { printf "%s,%s\n",save,$2 }' >> info.csv – nightcrawler Nov 9 '16 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.