I sample "winning" sets of 60 game cards such as this one and extract the card names and count and format it for use with some software. The output format is:


Step 1

I start with the text version of the set using the link provided on the page(next to print) and keep the data(2 columns separated by a space) removing the Sideboard cards.

Step 2

For each card name, I search a local list file containing a list of cards properties separated by semicolons and match the card name on the second field to extract the first field which is the ID number for the corresponding card(lines were shortened here as only the first fields matter):

ID;Full Name;...
0;Air Elemental;1;Air Elemental;0800h;;;;;;;
1;Ancestral Recall;1;Ancestral Recall;0800h;;;;;;;;
2;Animate Artifact;1;Animate Artifact;0800h;;;;;;;
10780;Jace, the Mind Sculptor;1;Jace, the Mind Sculptor;0008h;;;;;;;;
10871;Aura Finesse;0;Aura Finesse;0008h;;;;;;;

...I then prefix each line from step 1 with .ID, making sure the 3 columns are separated with a tabulation. The final output file looks like this(I'm not concerned with the header part):

;MY1 - Shardless BUG - #1
;User E-Mail
;4th Edition

.12516  2   Baleful Strix
.12533  4   Shardless Agent
.12700  4   Deathrite Shaman
.9038   4   Tarmogoyf
.10780  2   Jace, the Mind Sculptor
.12004  2   Liliana of the Veil
.12     2   Bayou
.10747  2   Creeping Tar Pit
.12645  4   Abrupt Decay
.1547   4   Brainstorm
.2285   3   Force of Will
.1456   1   Hymn to Tourach
.13650  1   Toxic Deluge
.9286   3   Thoughtseize
.239    1   Swamp
.1486   4   Polluted Delta
.258    3   Underground Sea
.10706  4   Verdant Catacombs
.3329   2   Wasteland
.1      4   Ancestral Recall
.10612  2   Misty Rainforest
.252    2   Tropical Island

Considering the aforementioned source text file and the list file, how can produce the output file with the required format?

  • This is really a programming problem of the kind that for which most shell dialects are not suited. I suggest you look at using Python or some similar such scripting language with which you're familiar. And if you still need help with that go to stackoverflow because this is a programming problem not a Unix one.
    – Giel
    Aug 30, 2014 at 12:32
  • while it is technically possible to do this with a shell-script this is the kind of task that is far from easy to achieve using that. I think your own answer below actually illustrates rather well that the kind of code you'll end up getting in shell-script does not express the intent of your code very well. In fact your problem description reads a lot like an SQL query joining two tables. The reason I suggested Python ("or some similar such scripting language") was that it's easy to perform small text-processing tasks in that, as well as perform cross-table matching.
    – Giel
    Sep 1, 2014 at 8:19

1 Answer 1


So what I did is that I trimmed the list file down to the first two values (ID;Name), then I used this script on the source text file:

#!/usr/bin/env bash

dos2unix "$1"
sed -e '/Sideboard/,$d' "$1" -e '/^$/,$d' | tee source_strip | cut -d ' ' -f 2- >temp
while IFS= read f; do
  sed -n "s/\([0-9]*\);$f$/\.\1/p" list
done <temp >IDs
sed -ni 's/^\([0-9]*\) \([a-zA-Z]*\)/\1\t\2/p' source_strip
paste IDs source_strip > final
cat final

Of course this assumes an ID number exists for every name search otherwise this won't properly work. Surely there is more elegant way to do this, but in this case it does work!

  • I should have removed the IDs, temp, and source_strip temporary files at the end of the script. Also using unix2dos on the final file if I'm going to use this in Win. The script doesn't always work with a quote in the name when searching the list file. Either not quoting the variable properly or dos control chars in the list file. But it helps!
    – user44370
    Sep 18, 2014 at 10:06

Your Answer

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