How can I remove a string after a specific character ONLY in a column/field in awk or bash?

You need to simply correct your regex.

awk '{sub(/;.*/,"",$2)} 1' Input_file

In case you have Input_file TAB delimited then try:

awk 'BEGIN{FS=OFS="\t"} {sub(/;.*/,"",$2)} 1' Input_file

Problem in OP's regex: OP's regex ;[*] is looking for ; and *(literal character) in 2nd field that's why its NOT able to substitute everything after ; in 2nd field. We need to simply give ;.* which means grab everything from very first occurrence of ; till last of 2nd field and then substitute with NULL in 2nd field.


An alternative solution using gnu sed:

sed -E 's/(^[^\t]*\t+[^;]*);[^\t]*/\1/' file
a   b   c
1   11  213
2   22  222
3   333 83838

Tags:

Awk

Sed