user134132523
user134132523

Reputation: 17

How to replace a character to another in a column of a text file using awk command in shell scripting

I have a file which looks a bit like

column1    column2    column3
1          45n8       005
2          125n05     007
3          002n012    009

How can I use awk to replace the 'n' in column2 with a '.' where the output will look like this:

column1    column2    column3
1          45.8       005
2          125.05     007
3          002.012    009

my attempt at this is awk 'BEGIN{OFS=",";}{gsub("n",".",$2);print}' file1.txt>file2.txt and its giving me a 'command not found' error

Upvotes: 0

Views: 769

Answers (1)

Kent
Kent

Reputation: 195289

you don't want to replace all n in a row, but only the col2, so you could:

awk 'NR>1{sub(/n/,".",$2)}7' file

If you want the output format looks better:

awk 'NR>1{sub(/n/,".",$2)}7' file|column -t

Upvotes: 1

Related Questions