I have an ethereum log file from which I would like to extract transaction hash located between a particular string Tx(0x and ) and there are lot of lines before and after it.

Input File

I0623 10:56:00.025283 miner/worker.go:516] commit new work on block 1903 with 0 txs & 0 uncles. Took 621.171µs
I0623 10:56:00.025349 miner/unconfirmed.go:105] 🔗  mined block #1897 [21f018be…] reached canonical chain
I0623 10:56:07.270041 rlp/encode.go:109] 02
I0623 10:56:07.270908 rlp/encode.go:109] 03
I0623 10:56:15.706523 internal/ethapi/api.go:1034] Debug message
I0623 10:56:15.738777 internal/ethapi/api.go:1052] Tx(0x22fb16e22be2123a161c887739d73ecca56a417ce176398da5a766067acb40ff) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29
I0623 10:56:15.739130 rlp/encode.go:109] 12
I0623 10:56:15.893529 internal/ethapi/api.go:1034] Debug message
I0623 10:56:15.896059 internal/ethapi/api.go:1052] Tx(0xa39474928ba5af3ef62950e0ce3b7a94f57eaf7fe9b7019d161de28f38d312c2) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29
I0623 10:56:15.896365 rlp/encode.go:109] 12
I0623 10:56:16.042498 internal/ethapi/api.go:1034] Debug message
I0623 10:56:16.044437 internal/ethapi/api.go:1052] Tx(0x953d94df1760f4b97377874a4cb3051927619e3cc29f38646c48913fb077d48e) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29
I0623 10:56:16.044604 rlp/encode.go:109] 12
I0623 10:56:16.156818 internal/ethapi/api.go:1034] Debug message
I0623 10:56:16.158712 internal/ethapi/api.go:1052] Tx(0x3feeda56a2c96663198c572685d574057cfa1ca92c9c0598e125d8d9cd3fc9f2) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29
I0623 10:56:16.159084 rlp/encode.go:109] 12
I0623 10:56:16.262574 internal/ethapi/api.go:1034] Debug message
I0623 10:56:16.264259 internal/ethapi/api.go:1052] Tx(0x12eaf5c4af1eb46a12d1bc642a59bf229e1ea7c1c941c1dfa3426e0b89f1a737) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29
I0623 10:56:16.264575 rlp/encode.go:109] 12
I0623 10:56:16.368707 internal/ethapi/api.go:1034] Debug message
I0623 10:56:16.370449 internal/ethapi/api.go:1052] Tx(0x3258bd0071968546584be0dfc63df130c3d71c6fcf41cf3caed3475c7c1d1a8e) to (Debug message): 0x36c282bd92e0a8aece9886ac5e05d9432f3b6a29

Output File


2 Answers 2


Solution using awk with multiple delimiters:

awk -F 'Tx\\(0x|)' '/Tx/ {print $2}' <your_file>


  • -F 'Tx\\(0x|)': use delimiters Tx(0x and )
  • /Tx/: only print lines containing Tx
  • {print $2}: print second element

If only one Tx... per line:

sed -n 's/.*Tx(0x\(.*\)) to.*/\1/p' logfile > outfile

Option -n to supress output for lines without match

  • thanks a lot @farax but this "grep -o '\Tx(.*) ' logMinter.log | sed -e 's/Tx(0x//;s/) //' > transactionHash" is giving me the desired result Commented Jun 23, 2017 at 7:07
  • 1
    @VarunGupta I slightly changed the answer of farax to make it work and you don't need grep or pipe.
    – Philippos
    Commented Jun 23, 2017 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .