From 112668f10c2aa3983282f40fd375fbaf2685f789 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 1 Jul 2017 17:29:09 +0100 Subject: [PATCH] examples: onelinecsv --- examples/onelinecsv.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 examples/onelinecsv.hs diff --git a/examples/onelinecsv.hs b/examples/onelinecsv.hs new file mode 100755 index 000000000..cfd60a31d --- /dev/null +++ b/examples/onelinecsv.hs @@ -0,0 +1,33 @@ +#!/usr/bin/env stack +{- stack runghc + --package hledger-lib + --package text +-} +{- example of generating one CSV line per txn. assumes hledger-lib 1.0+ -} + +import Control.Monad +import qualified Data.Text as T +import Data.List +import Hledger + +main :: IO () +main = do + Right j <- readJournalFile Nothing Nothing False "examples/sample.journal" + putStrLn $ intercalate ", " $ [ + "date" + ,"description" + ,"account1" + ,"amount1" + ,"account2" + ,"amount2" + ] + forM_ (jtxns j) $ \t -> do + let (p1:p2:_) = tpostings t + putStrLn $ intercalate ", " $ map quoteIfNeeded [ + show $ tdate t + ,T.unpack $ tdescription t + ,T.unpack $ paccount p1 + ,show $ pamount p1 + ,T.unpack $ paccount p2 + ,show $ pamount p2 + ]