justaguy
justaguy

Reputation: 3022

add character to specific field in sed or awk

I am trying to add a + in a specific field $4 in a tab delineated file. Thank you :).

input

chr12   9221325 9221448 chr12:9221325-9221448   A2M
chr12   9222330 9222419 chr12:9222330-9222419   A2M
chr12   9223073 9223184 chr12:9223073-9223184   A2M

desired output

chr12   9221325 9221448 +   A2M
chr12   9222330 9222419 +   A2M
chr12   9223073 9223184 +   A2M

sed so far (no specific to field)

sed 's/$/\t+/' < input > output

Upvotes: 1

Views: 426

Answers (1)

Lars Fischer
Lars Fischer

Reputation: 10149

Use awk:

awk 'BEGIN{FS=OFS="\t"} {$4="+"; print}' yourfile

It sets the fourth field to + and then print the fields.

Upvotes: 5

Related Questions