Saturday, October 18, 2008

Add column value based on condition - awk patterns


Input file release_3v2.txt contains the details of the tickets (CR) going in release 3.2. It contains the CR name, module name and "reported by".

$ cat release_3v2.txt
R92892|main_module|lneubuss
R92851|stiching|lneubuss
I92390|Integration|pshanes
R91234|mapper|lneubuss
I90876|gen|pshanes

Required: The ticket numbers starting with R is related to "regression test" and to be assigned to "lanish@jaduks.com", and integration related tickets (ticket numbers starting with I) is to be assigned to "minz@jaduks.com".
To be specific, we need to add the "Assigned to" column as the 4th column based on the ticket type(I or R).

Awk patterns to be used here:

pattern ? pattern : pattern

Meaning: If the first pattern is true then the pattern used for testing is the second pattern, other-wise it is the third. Only one of the second and third patterns is evaluated.

The awk solution:

$ awk '
BEGIN {FS=OFS="|"; print "Ticket|Module|LoggedBy|AssignedTo"}
($4=$1~/^R/?"lanish@jaduks.com":$1~/^I/?"minz@jaduks.com":"")||1
' release_3v2.txt

Output:

Ticket|Module|LoggedBy|AssignedTo
R92892|main_module|lneubuss|lanish@jaduks.com
R92851|stiching|lneubuss|lanish@jaduks.com
I92390|Integration|pshanes|minz@jaduks.com
R91234|mapper|lneubuss|lanish@jaduks.com
I90876|gen|pshanes|minz@jaduks.com

And if there is only two choices i.e. tickets starting with R to be assigned to "lanish@jaduks.com" and all others to "minz@jaduks.com"; the awk solution would be.

$ awk '
BEGIN {FS=OFS="|"; print "Ticket|Module|LoggedBy|AssignedTo"}
$4=$1~/^R/?"lanish@jaduks.com":"minz@jaduks.com"
' release_3v2.txt

Output:

Ticket|Module|LoggedBy|AssignedTo
R92892|main_module|lneubuss|lanish@jaduks.com
R92851|stiching|lneubuss|lanish@jaduks.com
I92390|Integration|pshanes|minz@jaduks.com
R91234|mapper|lneubuss|lanish@jaduks.com
I90876|gen|pshanes|minz@jaduks.com

No comments:

© Jadu Saikia www.UNIXCL.com