6

I am testing some new code, and need to sort the output of listtransactions. Ultimately I need to verify that there are no duplicate txids.

How do I output ./bitcoind -datadir=1 listtransactions "*" to a logfile, or sort it directly in the terminal window?

  • Are you asking for a shell script? Or code? If code, what language? – David Schwartz Sep 7 '11 at 20:02
  • Either, and whichever has the smallest learning curve =) – Alex Waters Sep 8 '11 at 3:02
  • "Sort the output" is not really specific. Outputting to a file is done with the > operator. If you want to do more that just output to a file, you must use a scripting language. It would help if you tell us what you want to do and in what language. – nmat Sep 8 '11 at 5:20
  • The command I am executing in my question outputs text into the terminal window. I would like to parse that text to compare the lines containing txid. I don't know what language would be best for this. – Alex Waters Sep 8 '11 at 5:27
  • What do you mean compare? If you want to analyze the data yourself, you can output to a file and manually select what you need: ./bitcoind -datadir=1 listtransactions "*" > transactions.log If it is a more automated procedure like removing duplicates or filtering some transactions, we can give you a few pointers/examples on a scripting language. – nmat Sep 8 '11 at 5:32
10

You can run the listtransaction JSON output through this little python script (save it as jsonTOCSV.py). That will produce csv (comma separated value) output that Excel or Google Spreadsheets can import, both of which can sort by columns (if I recall correctly).

So:

bitcoind listtransactions | jsonTOCSV.py > transactions.csv

#!/usr/bin/env python
#
# Reads an array of JSON objects and writes out CSV-format,
# with key names in first row.
# Columns will be union of all keys in the objects.
#

import csv
import json
import sys

json_string = sys.stdin.read()
json_array = json.loads(json_string)

columns = set()
for item in json_array:
  columns.update(set(item))

writer = csv.writer(sys.stdout)
writer.writerow(list(columns))
for item in json_array:
  row = []
  for c in columns:
    if c in item: row.append(str(item[c]))
    else: row.append('')
  writer.writerow(row)
  • 1
    Thank you. This helps me sort the transactions and get exposure to Python/JSON. – Alex Waters Sep 8 '11 at 5:30
3

I am not sure what you mean by sorting, but here is a little python script to check for duplicate txid's:

import json
from sys import argv, stdin

file = stdin.read() #reads transaction list from input
json_file = json.loads(file)

#counts the number of duplicate txid's
txid_list = [transaction["txid"] for transaction in json_file]
print "There are", len(txid_list) - len(set(txid_list)), "duplicates"

Let's suppose that you save this to a file named check_duplicates.py. Now you just need to output the transactions to a file and feed it to the script:

$ ./bitcoind -datadir=1 listtransactions "*" > transactions.log
$ python check_duplicates.py < transactions.log
There are N duplicates

Depending on what you need, other sorting operations can be done. It's easy to manage json with python.

2

I don't believe bitcoind has any kind of a sort option. The output of bitcoind listtransactions is in chronological order, but the output of your command could be written to a text file and parsed by any number of simple scripts. I don't have one offhand but the output is in JSON which most languages (including Python, my favorite) handle quite nicely. Once you bring the JSON data in it should be a simple matter of sorting the transactions array.

Edit: Depending on what data you're trying to extract and in what format, you may be able to do something with Gavin Andresen's bitcointools scripts.

0

This shell command will show each transaction ID once only, preceded by a count of the number of times it appears. The IDs which occur most often will be at the end:

bitcoind listtransactions '*' 999999 | grep '"txid"' | uniq -c | sort -n

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.