Krishna S
Krishna S

Reputation: 71

Using head and tail in UNIX to extract specific transaction

I have a question regarding using the head and tail commands in UNIX to truncate a specific transaction from a huge transaction log.

head -X <<<filename>>> | tail -Y > <<<Truncatedfile>>>

where X is the number of lines I want from the beginning of the file and Y is the number of lines I want from the bottom of the file.

How can I modify this code to have the truncated file with just the transactions for a unique transaction ID? For example:- The file contains transaction logs for n number of transaction IDs in a sequence. So, if I only need logs extracted for just 1 single transaction ID how to modify the above code?

Upvotes: 1

Views: 3964

Answers (1)

sehe
sehe

Reputation: 393537

You wouldn't modify the above code, instead you'd

grep -w transactionid filename

Assuming that the transactionid appears as a separate word (-w)

Edit You can include some context lines (this includes 10 lines after the match:)

grep -w -A 10 transactionid filename

Alternatively,

grep -vw transactionid filename

Simple hides all lines NOT containing the transaction id. This close to equivalent to doing sed -e '/transactionid/!d'.


To print lines 5-12

sed -n '5,12p' filename

Upvotes: 2

Related Questions