24

I have a file like this:

2018.01.02;1.5;comment 1
2018.01.04;2.75;comment 2
2018.01.07;5.25;comment 4
2018.01.09;1.25;comment 7

I want to replace all dots . in the second column with a comma , as I would with sed 's/\./\,/g' file how can I use sed or preferably awk to only apply this for the second column, so my output would look like this:

2018.01.02;1,5;comment 1
2018.01.04;2,75;comment 2
2018.01.07;5,25;comment 4
2018.01.09;1,25;comment 7

3 Answers 3

36
$ awk 'BEGIN{FS=OFS=";"} {gsub(/\./, ",", $2)} 1' ip.txt
2018.01.02;1,5;comment 1
2018.01.04;2,75;comment 2
2018.01.07;5,25;comment 4
2018.01.09;1,25;comment 7
  • BEGIN{} this block of code will be executed before processing any input line
  • FS=OFS=";" set input and output field separator as ;
  • gsub(/\./, ",", $2) for each input line, replace all the . in 2nd field with ,
  • 1 is an awk idiom to print contents of $0 (which contains the input record)
0
13
sed 's/\./,/3' file

replace the third occurence of the dot

0
11

Done by below method using awk

Command: awk -F ";" '{gsub(/\./,",",$2);print $1";"$2";"$3}' filename

output

2018.01.02;1,5;comment 1
2018.01.04;2,75;comment 2
2018.01.07;5,25;comment 4
2018.01.09;1,25;comment 7

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .