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

<pre><code>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"
</code></pre>

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

<pre><code>$ ./bitcoind -datadir=1 listtransactions "*" &gt; transactions.log
$ python check_duplicates.py &lt; transactions.log
There are N duplicates
</code></pre>

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

CC BY-SA 3.0
2011-09-07T23:41:07.260Z
2011-09-07T23:48:03.347Z
2011-09-07T23:48:03.347Z
nmat
<p>I don't believe bitcoind has any kind of a sort option. The output of <code>bitcoind listtransactions</code> 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 <a href="http://docs.python.org/library/json.html" rel="nofollow">Python</a>, my favorite) handle quite nicely. Once you bring the JSON data in it should be a simple matter of <a href="http://wiki.python.org/moin/HowTo/Sorting" rel="nofollow">sorting</a> the transactions array.</p>

<p><strong>Edit:</strong> <em>Depending on what data you're trying to extract and in what format, you may be able to do something with Gavin Andresen's <a href="https://github.com/gavinandresen/bitcointools" rel="nofollow">bitcointools</a> scripts.</em></p>

CC BY-SA 3.0
2011-09-07T20:05:11.293Z
2011-09-07T21:41:22.743Z
2011-09-07T21:41:22.743Z
David Perry
<p>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:</p>

<pre><code>bitcoind listtransactions '*' 999999 | grep '"txid"' | uniq -c | sort -n
</code></pre>

CC BY-SA 3.0
2012-02-20T19:54:45.123Z
2012-02-20T19:54:45.123Z
Chris Moore
<p>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).</p>

<p>So:</p>

<blockquote>
  <p>bitcoind listtransactions | jsonTOCSV.py > transactions.csv</p>
</blockquote>

<pre><code>#!/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)
</code></pre>

CC BY-SA 3.0
2011-09-08T05:30:38.460Z
Thank you. This helps me sort the transactions and get exposure to Python/JSON.
Alex Waters
CC BY-SA 3.0
2011-09-08T01:34:10.047Z
2011-09-08T01:34:10.047Z
gavinandresen
<p>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.</p>

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

CC BY-SA 3.0
2011-09-07T20:02:34.307Z
Are you asking for a shell script? Or code? If code, what language?
David Schwartz
CC BY-SA 3.0
2011-09-08T03:02:39.893Z
Either, and whichever has the smallest learning curve =)
Alex Waters
CC BY-SA 3.0
2011-09-08T05:20:25.080Z
"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
CC BY-SA 3.0
2011-09-08T05:27:49.350Z
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
CC BY-SA 3.0
2011-09-08T05:32:45.747Z
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
<ubuntu><bitcoind>
How do I sort the output of listtransactions "*"?
Alex Waters
# How do I sort the output of listtransactions "*"?

(Asked by: username_0 on 2011-09-07)

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?

**Tags:** ubuntu, bitcoind

> **Comments:**
> **username_1**: Are you asking for a shell script? Or code? If code, what language?  
> **username_0**: Either, and whichever has the smallest learning curve =)  
> **username_2**: "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.  
> **username_0**: 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.  
> **username_2**: 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.  

---

## 4 answers

### Answer by username_5 on 2011-09-08. Score: 10 | Accepted answer

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)

```

> **Comments:**
> **username_0**: Thank you. This helps me sort the transactions and get exposure to Python/JSON.  

---

### Answer by username_2 on 2011-09-07. Score: 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.


---

### Answer by username_3 on 2011-09-07. Score: 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](http://docs.python.org/library/json.html), my favorite) handle quite nicely. Once you bring the JSON data in it should be a simple matter of [sorting](http://wiki.python.org/moin/HowTo/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](https://github.com/username_5/bitcointools) scripts.*


---

### Answer by username_4 on 2012-02-20. Score: 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

```


---

