Remove period (.) From specific columns with gsub and awk

I want to remove the dot (.) Only from the 4th and 5th columns of the table.

input 1 10057 . A AC 1 10146 . AC. A 1 10177 . A AC 1 10230 . AC .A,AN 1 10349 . CCCTA C,CCCTAA. 1 10389 . .AC A,AN desired output 1 10057 . A AC 1 10146 . AC A 1 10177 . A AC 1 10230 . AC A,AN 1 10349 . CCCTA C,CCCTAA 1 10389 . AC A,AN 

So, I tried the following command.

 awk 'BEGIN {OFS=FS="\t"} {gsub("\.","",$4);gsub("\.","",$5)}1' input 

and I got this result (the entire 4th and 5th columns were deleted).

 1 10057 . 1 10146 . 1 10177 . 1 10230 . 1 10349 . 1 10389 . 

Could you please indicate where I need to change? Thanks in advance.

+7
unix awk gsub
source share
1 answer

When you use a string to store RE (for example, "\." ), The string is parsed twice β€” once when the script is read by awk and then executed again by awk. As a result, you need to escape the RE metacharacters twice (for example, "\\." ).

The best solution in all respects is not specifying RE as a string, but specifying it as a RE constant instead of using the appropriate delimiters, for example. /\./ :

 awk 'BEGIN {OFS=FS="\t"} {gsub(/\./,"",$4);gsub(/\./,"",$5)}1' input 
+9
source share

All Articles